Detecting scroll finish/end

2019-06-22 15:05发布

I'd like to detect when a user has stopped scrolling a page/element. This may be tricky as recently enhancements to OSX's scrolling behaviour creates this new inertia effect. Is there an event fired?

The only other solution I can think of is using a interval to pick up when the scroll position of a page/element no longer changes, for example:

var element     = $( el );
var previous    = element.scrollLeft();
var current;

element.scroll( function(event)
{
    current = element.scrollLeft();

    if ( current === previous )
    {
        // user has stopped scrolling
    }

    previous    = current;
});

2条回答
淡お忘
2楼-- · 2019-06-22 15:33

Take a look at this, its jquery, and working well for me, hope to work for you.

Also there is an other scroll event code in here.

查看更多
beautiful°
3楼-- · 2019-06-22 15:40

This may be useful.

// Setup isScrolling variable
var isScrolling;

// Listen for scroll events
window.addEventListener('scroll', function ( event ) {

    // Clear our timeout throughout the scroll
    window.clearTimeout( isScrolling );

    // Set a timeout to run after scrolling ends
    isScrolling = setTimeout(function() {

        // Run the callback
        console.log( 'Scrolling has stopped.' );

    }, 66);

}, false);
查看更多
登录 后发表回答