【问题标题】:scrollstop event without jQuery mobile [duplicate]没有jQuery mobile的滚动停止事件[重复]
【发布时间】:2015-10-04 09:03:16
【问题描述】:
当用户停止滚动时,我试图调用一个函数。但我不想在 jQuery mobile 中使用“scrollstop”,因为我在其他任何地方都不需要 jQuery mobile。普通的 jQuery 或 JS 中是否有等价的事件?
【问题讨论】:
标签:
javascript
jquery
events
jquery-mobile
scroll
【解决方案1】:
我找到的唯一答案是仍在使用 jquery,并且由于某些原因在我的项目中不起作用。
这是我的版本,不需要 jQuery,尽情享受吧!
注意:这里是一个sn-p,但是由于它是嵌入的,它会延迟事件很多
var lastTimeScrolled = null;
var theElementYouCareAbout = document;
var intervalMilliSeconds = 500; // interval goes here
theElementYouCareAbout.onscroll = function(){
if (performance.now() - lastTimeScrolled > intervalMilliSeconds){
var intervalScroll = setInterval(function(){
if (performance.now() - lastTimeScrolled > intervalMilliSeconds){
doSomething();
clearInterval(intervalScroll);
}
}.bind(intervalScroll).bind(intervalMilliSeconds), 100);
}
lastTimeScrolled = performance.now();
}.bind(intervalMilliSeconds);
function doSomething (){
alert('You just stopped scrolling :)');
}
<div style="height: 200vh">
Scroll me!
</div>