mouseWheel: function () {
function mousewheel(obj, upfun, downfun) {
if (document.attachEvent) {
obj.attachEvent("onmousewheel", scrollFn)
} else {
if (document.addEventListener) {
obj.addEventListener("mousewheel", scrollFn, false);
obj.addEventListener("DOMMouseScroll", scrollFn, false)
}
}
function scrollFn(e) {
var ev = e || window.event;
var dir = ev.wheelDelta || ev.detail;
if (ev.preventDefault) {
ev.preventDefault()
} else {
ev.returnValue = false
}
if (dir == -3 || dir == 120) {
upfun()
} else {
downfun()
}
}
}
var that = this;
mousewheel($("." + that.bigimg)[0], function () {
if ($("." + that.bigimg).innerWidth() > $("body").width() - 20) {
return
}
if ($("." + that.bigimg).innerHeight() > $("body").height() - 50) {
return
}
var zoomHeight = $("." + that.bigimg).innerHeight() * 1.03;
var zoomWidth = $("." + that.bigimg).innerWidth() * 1.03;
$("." + that.bigimg).css({height: zoomHeight + "px", width: zoomWidth + "px"})
}, function () {
if ($("." + that.bigimg).innerWidth() < 100) {
return
}
if ($("." + that.bigimg).innerHeight() < 100) {
return
}
var zoomHeight = $("." + that.bigimg).innerHeight() / 1.03;
var zoomWidth = $("." + that.bigimg).innerWidth() / 1.03;
$("." + that.bigimg).css({height: zoomHeight + "px", width: zoomWidth + "px"})
})
}
检测滚动方向,闭包保证变量不污染外部
(function () {
var previousScroll = 0;
$(window).scroll(function(){
var currentScroll = $(this).scrollTop();
if (currentScroll > previousScroll){
alert('down');
} else {
alert('up');
}
previousScroll = currentScroll;
});
}()); //run this anonymous function immediately
评论区