Can one use [removed] method to include detection of scroll direction?

后端 未结 3 1643
走了就别回头了
走了就别回头了 2020-11-30 03:36

Can one use Window.Onscroll method to include detection of scroll direction?

相关标签:
3条回答
  • 2020-11-30 04:00

    I had trouble making this work in ie8 (although it is compliant for ie9, FF and Chrome) - all scrolls seem to be detected as horizontal.

    Here is a modified script demo that also works in ie8 and may cover a few more browsers.

    function scrollFunc(e) {
    
       function getMethod() {
            var x = 0, y = 0;
            if ( typeof( window.pageYOffset ) == 'number' ) {
              x = window.pageXOffset;
              y = window.pageYOffset;
            } 
    
            else if( document.body && (document.body.scrollLeft || document.body.scrollTop ) ) {
              x = document.body.scrollLeft;
              y = document.body.scrollTop;
            } 
    
            else if( document.documentElement && (document.documentElement.scrollLeft ||     document.documentElement.scrollTop ) ) {
              x = document.documentElement.scrollLeft;
              y = document.documentElement.scrollTop;
            }
    
             return [x, y];
        }
    
        var xy = getMethod();            
        var xMethod = xy[0];           
        var yMethod = xy[1];
    
        if ( typeof scrollFunc.x == 'undefined' ) {
           scrollFunc.x = xMethod;
           scrollFunc.y = yMethod;
        }
    
        var diffX = scrollFunc.x - xMethod;
        var diffY = scrollFunc.y - yMethod;
    
    
        if( diffX<0 ) {
           // Scroll right
        } else if( diffX>0 ) {
           // Scroll left
        } else if( diffY<0 ) {
           // Scroll down
        } else if( diffY>0 ) {
           // Scroll up
        } else {
           // First scroll event
        }
        scrollFunc.x = xMethod;
        scrollFunc.y = yMethod;
    }
    
    window.onscroll=scrollFunc​
    
    0 讨论(0)
  • 2020-11-30 04:02

    With jquery, you can also register a custom scroll event which supplies the scroll change as an argument to the event handler:

    var previous_scroll = $(window).scrollTop();
    
    $(window).on('scroll', function() {
        var scroll = $(window).scrollTop(),
            scroll_change = scroll - previous_scroll;
        previous_scroll = scroll;
        $(window).trigger('custom_scroll', [scroll_change]);
    });
    

    Then instead of scroll, bind to custom_scroll:

    $(window).on('custom_scroll', function pos(e, scroll_change) {
        console.log(scroll_change);
    });
    
    0 讨论(0)
  • 2020-11-30 04:17

    If you record the scrollX and scrollY on page load and each time a scroll event occurs, then you can compare the previous values with the new values to know which direction you scrolled. Here's a proof of concept:

    function scrollFunc(e) {
        if ( typeof scrollFunc.x == 'undefined' ) {
            scrollFunc.x=window.pageXOffset;
            scrollFunc.y=window.pageYOffset;
        }
        var diffX=scrollFunc.x-window.pageXOffset;
        var diffY=scrollFunc.y-window.pageYOffset;
    
        if( diffX<0 ) {
            // Scroll right
        } else if( diffX>0 ) {
            // Scroll left
        } else if( diffY<0 ) {
            // Scroll down
        } else if( diffY>0 ) {
            // Scroll up
        } else {
            // First scroll event
        }
        scrollFunc.x=window.pageXOffset;
        scrollFunc.y=window.pageYOffset;
    }
    window.onscroll=scrollFunc
    
    0 讨论(0)
提交回复
热议问题