当用户尝试滚动时,是否可以拥有jQuery或javascript侦听器,即使网页不可滚动?当用户尝试向下滚动时,我需要执行一些代码,即使网页足够短以至于没有滚动。
请注意,此解决方案最需要用于触摸屏。
更新当用户尝试向上滚动而不是向下滚动时,我需要执行不同的代码。
答案 0 :(得分:0)
更新:使用on
代替bind
并插入滚动方向。
还添加了DOMMouseScroll
,以便在Firefox中使用。
只需使用:
$(window).on('mousewheel DOMMouseScroll',function(e){
// To catch up or down scroll:
e = e.originalEvent;
// delta == 1 = down, delta == -1 = up;
var delta = e.wheelDelta > 0 || e.detail < 0 ? 1 : -1;
}
如果您愿意,可以将其绑定到其他内容。
答案 1 :(得分:0)
$(window).on('mousewheel', function (e) {
if (e.originalEvent.wheelDeltaY < 0) {
alert("Scrolled down");
} else if (e.originalEvent.wheelDeltaY > 0) {
alert("Scrolled up");
}
});
似乎不同的浏览器会为您提供不同的增量。 此解决方案无法在IE等所有浏览器中使用!
我真的建议你看看Brandon Aaron用于jQuery的Mousewheel-Plugin,它也适用于IE:https://github.com/brandonaaron/jquery-mousewheel
使用该插件,您应该能够获得更好的解决方案:
$(window).on('mousewheel', function(event, delta, deltaX, deltaY) {
if (deltaY < 0) {
alert("Scrolled down");
} else if (deltaY > 0) {
alert("Scrolled up");
}
});
答案 2 :(得分:0)
感谢您的回答和建议,但我发现了一个适合我的版本: Get mouse wheel events in jQuery?
$(document).ready(function(){
$('body').bind('mousewheel', function(e){
if(e.originalEvent.wheelDelta /120 > 0) {
console.log('scrolling up !');
}
else{
console.log('scrolling down !');
}
});
});