在jQuery中检测用户向下滚动或向上滚动 [英] Detect user scroll down or scroll up in jQuery
本文介绍了在jQuery中检测用户向下滚动或向上滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能的重复:
在jquery中区分向上/向下滚动?
是否可以检测用户是向下滚动还是向上滚动?
Is it possible to detect if user scroll down or scroll up ?
示例:
$(window).scroll(function(){
// IF USER SCROLL DOWN
DO ACTION
// IF USER SCROLL UP
DO ANOTHER ACTION
});
谢谢
推荐答案
要区分 jQuery 中的向上/向下滚动,您可以使用:
To differentiate between scroll up/down in jQuery, you could use:
var mousewheelevt = (/Firefox/i.test(navigator.userAgent)) ? "DOMMouseScroll" : "mousewheel" //FF doesn't recognize mousewheel as of FF3.x
$('#yourDiv').bind(mousewheelevt, function(e){
var evt = window.event || e //equalize event object
evt = evt.originalEvent ? evt.originalEvent : evt; //convert to originalEvent if possible
var delta = evt.detail ? evt.detail*(-40) : evt.wheelDelta //check for detail first, because it is used by Opera and FF
if(delta > 0) {
//scroll up
}
else{
//scroll down
}
});
此方法也适用于具有 overflow:hidden
的 div.
This method also works in divs that have overflow:hidden
.
我在 FireFox、IE 和 Chrome 中成功测试了它.
I successfully tested it in FireFox, IE and Chrome.
这篇关于在jQuery中检测用户向下滚动或向上滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文