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

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

问题描述

我怎么知道何时停止使用Java脚本滚动?

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

这将启动超时并等待150ms。如果在此期间发生了新的 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天全站免登陆