我可以在 jQuery 上为滚动事件的开始和结束声明逻辑吗? [英] Can I declare logic on jQuery for the start and end of a scrolling event?
本文介绍了我可以在 jQuery 上为滚动事件的开始和结束声明逻辑吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想设置用户何时开始滚动页面以及滚动完成后的逻辑,我该如何实现?
I would like to setup logic for when the user begins scrolling around the page and after the scrolling is finished, how can I accomplish this?
我想避免以下内容,因为这意味着我的逻辑将不必要地重复触发
I want to avoid the below as it means my logic will be fired repeatedly unnecessarily
$(window).scroll(function(){
console.log("scrolling");
});
推荐答案
你可以尝试定义你自己的去抖动事件.一个(非常粗略的)实现看起来像这样:
You can try defining you're own debounced events. A (very crude) implementation would look something like this:
var t, l = (new Date()).getTime();
$(window).scroll(function(){
var now = (new Date()).getTime();
if(now - l > 400){
$(this).trigger('scrollStart');
l = now;
}
clearTimeout(t);
t = setTimeout(function(){
$(window).trigger('scrollEnd');
}, 300);
});
参见:http://www.jsfiddle.net/yijiang/fGmbe/ 现场演示
See: http://www.jsfiddle.net/yijiang/fGmbe/ for a live demo
这篇关于我可以在 jQuery 上为滚动事件的开始和结束声明逻辑吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文