Detect if user is scrolling

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

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

4条回答
  •  暖寄归人
    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

提交回复
热议问题