使用javascript滚动滚动条或鼠标滚轮后的火灾事件 [英] Fire event after scrollling scrollbars or mousewheel with javascript
问题描述
我想知道在使用滚动条或鼠标滚轮(或在触摸设备上轻扫)时,是否可以在滚动页面后触发事件。
I would like to know if it is possible to fire an event after the scrolling of a page, when using the scrollbar or mouse-wheel (or with a swipe on a touch device).
基本上,我想检测用户何时停止滚动,这样我就可以加载AJAX,而不是在滚动时加载。
Basically, I'd like to detect when the user has stopped scrolling so I can then AJAX-load, rather than loading while scrolling.
似乎 jQuery的
.scroll()
每次用户滚动时都会触发,并且让事件一直开火似乎很笨拙。是否有 .onScrollAfter()
,与 .onMouseUp()
同义吗?
It seems that jQuery's
.scroll()
is firing every time a user scrolls, and it seems clunky to have an event fire all the time. Is there such thing as .onScrollAfter()
, synonymous to the .onMouseUp()
?
我想知道这是否可行(或者如果一个函数已经存在)而不使用框架,尽管我会考虑一个;特别是 jQuery
。
I'd like to know whether this is possible (or if a function already exists) without using a framework, though I would consider one; especially jQuery
.
推荐答案
此事件不存在。您可以使用超时来模拟它:
This event does not exist. You can emulate it by using timeouts:
示例(概念代码):
(function() {
var timer;
/* Basic "listener" */
function scroll_finish(ev) {
clearTimeout(timer);
timer = setTimeout(scroll_finished, 200, ev);
//200ms. Too small = triggered too fast. Too high = reliable, but slow
}
window.onscroll = scroll_finish; // Or addEventListener, it's just a demo
// Fire "events"
var thingey = [];
function scroll_finished(ev) {
// Function logic
for (var i=0; i<thingey.length; i++) {
thingey[i](ev);
}
}
// Add listener
window.addScrollListener = function(fn) {
if (typeof fn === 'function') {
thingey.push(fn);
} else {
throw TypeError('addScrollListener: First argument must be a function.');
}
}
window.removeScrollListener = function(fn) {
var index = thingey.indexOf(fn);
if (index !== -1) thingey.splice(index, 1);
}
})();
这篇关于使用javascript滚动滚动条或鼠标滚轮后的火灾事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!