很久以前在这里已经问过这个问题:
但是,从来没有一个定论的答案(或者也许我根本无法正确搜索)。
是否可以检测scroll
事件是由用户触发还是由jQueryanimate
函数触发的?
我试图防止scroll
事件在执行以下操作时触发自身:
$(document).scroll(function(){
$("html").stop(true);
var number = 400; //some other stuff is happening here
clearTimeout(tout);
tout = setTimeout(function(){
if(top == $(document).scrollTop()){
$("html").animate({
scrollTop: (number),
easing: "easeInQuad",
duration: 110
});
}
},120);
});
这段代码似乎是合适的:
$('#scroller').scroll(function(e) {
if (e.originalEvent) {
console.log('scroll happen manual scroll');
} else {
console.log('scroll happen by call');
}
});
但是originalEvent
对象无法正确检测到动画触发。
还有其他方法吗?
也许:animated
选择器可以帮助您:
$('#scroller').scroll(function(e) {
if ($(this).is(':animated')) {
console.log('scroll happen by animate');
} else if (e.originalEvent) {
// scroll happen manual scroll
console.log('scroll happen manual scroll');
} else {
// scroll happen by call
console.log('scroll happen by call');
}
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句