How to detect horizontal scrolling in jQuery?

前端 未结 4 473
时光说笑
时光说笑 2020-12-01 07:53

How do I detect horizontal scrolling with jQuery?

This will get all scrolls:

$(window).scroll(function () {
    alert(\'in\');
});

相关标签:
4条回答
  • 2020-12-01 08:04

    This seems to work.

    var lastScrollLeft = 0;
    $(window).scroll(function() {
        var documentScrollLeft = $(document).scrollLeft();
        if (lastScrollLeft != documentScrollLeft) {
            console.log('scroll x');
            lastScrollLeft = documentScrollLeft;
        }
    });
    

    jsFiddle.

    0 讨论(0)
  • 2020-12-01 08:09

    An update that shows direction left or right in a horizontal scroll based on code above:

    var lastPos = 0;
    $(window).scroll(function() {
        var currPos = $(document).scrollLeft();
    
        if (lastPos < currPos) {
            console.log('scroll right');
        } 
        if (lastPos > currPos) 
        {
            console.log('scroll left');
        }
    
        lastPos = currPos;
    });
    

    Test it at http://jsfiddle.net/EsPk7/7/

    0 讨论(0)
  • 2020-12-01 08:20

    dunderwood's jsFiddle link didn't actually contain his code. I've forked his jsFiddle with what a mixture of what he posted here and what's on the jsFiddle:

    var lastPos = 0;
    $(window).scroll(function() {
        var currPos = $(document).scrollLeft();
    
        if (lastPos < currPos) {
            $('#current').html('Right');
        }
        if (lastPos > currPos) {
            $('#current').html('Left');
        }
    
        lastPos = currPos;
    });
    

    http://jsfiddle.net/kuVK8/

    0 讨论(0)
  • 2020-12-01 08:22
    window.onresize = function() { 
        if ( $("div").outerWidth() < $("div").get(0).scrollWidth ) {
            console.log("foo bar scrollbar");
        } else {
            console.log(" no scrolling ");
        }
    };
    
    0 讨论(0)
提交回复
热议问题