检测滚动结束/结束 [英] Detecting scroll finish/end

查看:84
本文介绍了检测滚动结束/结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检测用户何时停止滚动页面/元素。这可能很棘手,因为最近对OSX滚动行为的增强产生了这种新的惯性效应。是否有事件被触发?

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;
});


推荐答案

这个可能很有用。

// 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);

这篇关于检测滚动结束/结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆