Is there a vertical scroll event in jQuery

2019-01-19 13:32发布

I have a function that I bound with scroll() event, but the fact is I want the function to be triggered only in case of vertical scroll ( I have some horizontal scroll too).

I didn't see such possibility in the documentation of jQuery, might there be a trick to do so?

标签: jquery scroll
3条回答
太酷不给撩
2楼-- · 2019-01-19 14:03

You can also use .scrollTop to make a custom vertical scroll event.

var prevTop = 0;
$(document).scroll( function(evt) {
    var currentTop = $(this).scrollTop();
    if(prevTop !== currentTop) {
        prevTop = currentTop;
        console.log("I scrolled vertically.");
    }
});

Jquery .scrollTop()

查看更多
啃猪蹄的小仙女
3楼-- · 2019-01-19 14:12

There isn't a specific event, but you could test the .scrollLeft() position to see if it has moved from a previously stored position.

Something like this:

var prevLeft = 0;
$(document).scroll( function(evt) {
    var currentLeft = $(this).scrollLeft();
    if(prevLeft != currentLeft) {
        prevLeft = currentLeft;
        console.log("I scrolled horizontally.");
    }
});
查看更多
何必那么认真
4楼-- · 2019-01-19 14:16

This should work:

var prevLeft = 0;
$(document).scroll( function(evt) {
    var currentLeft = $(this).scrollLeft();
    if(prevLeft === currentLeft) {
        console.log("I scrolled vertically.");
    } 
    else {
        prevLeft = currentLeft;
    }
});
查看更多
登录 后发表回答