检测用户向下滚动或在jQuery中向上滚动

可能重复:
区分在jQuery中向上滚动/向下滚动?

是否有可能检测用户是否向下滚动或向上滚动?

示例:

$(window).scroll(function(){

    // IF USER SCROLL DOWN 
           DO ACTION

    // IF USER SCROLL UP 
           DO ANOTHER ACTION

});

谢谢


为了区分jQuery中向上/向下滚动,你可以使用:

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 overflow:hidden

我成功地在FireFox,IE和Chrome中测试了它。

链接地址: http://www.djcxy.com/p/83835.html

上一篇: Detect user scroll down or scroll up in jQuery

下一篇: How to iterate over the keys and values with ng