Detect if user is scrolling

后端 未结 4 1659
清酒与你
清酒与你 2020-11-30 22:59

How can I detect in javascript if the user is scrolling?

相关标签:
4条回答
  • 2020-11-30 23:19

    this works:

    window.onscroll = function (e) {  
    // called when the window is scrolled.  
    } 
    

    edit:

    you said this is a function in a TimeInterval..
    Try doing it like so:

    userHasScrolled = false;
    window.onscroll = function (e)
    {
        userHasScrolled = true;
    }
    

    then inside your Interval insert this:

    if(userHasScrolled)
    {
    //do your code here
    userHasScrolled = false;
    }
    
    0 讨论(0)
  • 2020-11-30 23:21

    You just said javascript in your tags, so @Wampie Driessen post could helps you.

    I want also to contribute, so you can use the following when using jQuery if you need it.

     //Firefox
     $('#elem').bind('DOMMouseScroll', function(e){
         if(e.detail > 0) {
             //scroll down
             console.log('Down');
         }else {
             //scroll up
             console.log('Up');
         }
    
         //prevent page fom scrolling
         return false;
     });
    
     //IE, Opera, Safari
     $('#elem').bind('mousewheel', function(e){
         if(e.wheelDelta< 0) {
             //scroll down
             console.log('Down');
         }else {
             //scroll up
             console.log('Up');
         }
    
         //prevent page fom scrolling
         return false;
     });
    

    Another example:

    $(function(){
        var _top = $(window).scrollTop();
        var _direction;
        $(window).scroll(function(){
            var _cur_top = $(window).scrollTop();
            if(_top < _cur_top)
            {
                _direction = 'down';
            }
            else
            {
                _direction = 'up';
            }
            _top = _cur_top;
            console.log(_direction);
        });
    });​
    
    0 讨论(0)
  • 2020-11-30 23:24
    window.addEventListener("scroll",function(){
        window.lastScrollTime = new Date().getTime()
    });
    function is_scrolling() {
        return window.lastScrollTime && new Date().getTime() < window.lastScrollTime + 500
    }
    

    Change the 500 to the number of milliseconds after the last scroll event at which you consider the user to be "no longer scrolling".

    (addEventListener is better than onScroll because the former can coexist nicely with any other code that uses onScroll.)

    0 讨论(0)
  • 2020-11-30 23:26

    Use an interval to check

    You can setup an interval to keep checking if the user has scrolled then do something accordingly.

    Borrowing from the great John Resig in his article.

    Example:

        let didScroll = false;
    
        window.onscroll = () => didScroll = true;
    
        setInterval(() => {
            if ( didScroll ) {
                didScroll = false;
                console.log('Someone scrolled me!')
            }
        }, 250);
    

    See live example

    0 讨论(0)
提交回复
热议问题