我怎么知道我什么时候停止滚动? [英] How do I know when I've stopped scrolling?

查看:20
本文介绍了我怎么知道我什么时候停止滚动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何知道我何时停止使用 Javascript 滚动?

How do I know when I've stopped scrolling using Javascript?

推荐答案

您可以为 scroll 事件添加事件处理程序并启动超时.类似的东西:

You can add an event handler for the scroll event and start a timeout. Something like:

var timer = null;
window.addEventListener('scroll', function() {
    if(timer !== null) {
        clearTimeout(timer);        
    }
    timer = setTimeout(function() {
          // do something
    }, 150);
}, false);

这将开始超时并等待 150 毫秒.如果在此期间发生了新的 scroll 事件,则中止计时器并创建一个新的计时器.如果没有,该函数将被执行.您可能需要调整时间.

This will start a timeout and wait 150ms. If a new scroll event occurred in the meantime, the timer is aborted and a new one is created. If not, the function will be executed. You probably have to adjust the timing.

还要注意 IE 使用不同的方式来附加事件侦听器,这应该给出一个很好的介绍:quirksmode - 高级事件注册模型

Also note that IE uses a different way to attach event listeners, this should give a good introduction: quirksmode - Advanced event registration models

这篇关于我怎么知道我什么时候停止滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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