我如何知道何时停止滚动Javascript [英] How do I know when I've stopped scrolling Javascript
问题描述
我怎么知道我什么时候停止使用Javascript滚动
How do I know when I've stopped scrolling using Javascript
非常感谢
推荐答案
您可以为滚动
事件添加事件处理程序并启动超时。类似于:
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。如果在此期间发生新的滚动
事件,则中止计时器并创建一个新计时器。如果不是,则执行该功能。您可能需要调整时间。
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
这篇关于我如何知道何时停止滚动Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!