Is there a vertical scroll event in jQuery

前端 未结 3 1655
囚心锁ツ
囚心锁ツ 2020-12-06 11:34

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

相关标签:
3条回答
  • 2020-12-06 11:49

    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()

    0 讨论(0)
  • 2020-12-06 11:57

    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.");
        }
    });
    
    • http://api.jquery.com/scrollLeft/
    0 讨论(0)
  • 2020-12-06 12:00

    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;
        }
    });
    
    0 讨论(0)
提交回复
热议问题