检测滚动事件是否在jQuery中手动触发 [英] Detect if a scroll event is triggered manually in jQuery

查看:140
本文介绍了检测滚动事件是否在jQuery中手动触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题早已在这里提出:



检测jquery事件触发器或用代码调用



但是从来没有得到任何结论(或者我根本无法正确搜索) / p>

是否可以检测一个滚动事件是否已被用户或jQuery animate function?



我试图阻止滚动事件触发自己在做这样的事情:

  $(document).scroll(function(){
$(html ).stop(true);
var number = 400; //其他一些东西在这里发生
clearTimeout(tout);
tout = setTimeout(function(){
if(top == $(document).scrollTop()){
$(html)。animate({
scrollTop:(number),
easing:easeInQuad,
硬脑膜:110
});
}
},120);
});

此代码似乎是合适的:

  $('#scroller')。scroll(function(e){
if(e.originalEvent){
console.log('scroll happen manual scroll' );
} else {
console.log('scroll sending by call');
}
});

但是, originalEvent 对象不能要正确地检测动画触发器。



有没有其他方法可以做到这一点?

解决方案

也许:动画选择器将帮助您:

  $('#croller')。scroll(function(e){
if($(this).is(':animated')){
console.log('scroll by animate' );
} else if(e.originalEvent){
//滚动发生手动滚动
console.log('scroll happen manual scroll');
} else {
//滚动发生在调用
console.log('滚动发生在调用');
}
});

演示


This question was already asked here a long time ago:

Detect jquery event trigger by user or call by code

But it has never been answered conclusively (or maybe I'm simply not able to search properly).

Is it possible to detect whether a scroll event has been triggered by the user or by the jQuery animate function?

I am trying to prevent the scroll event to trigger itself while doing something like this:

$(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);
});

This code seems to be suitable:

$('#scroller').scroll(function(e) {
    if (e.originalEvent) {
        console.log('scroll happen manual scroll');
    } else {
        console.log('scroll happen by call');
    }
});

But the originalEvent object isn't able to detect the animate trigger properly.

Is there any other way to do this?

解决方案

Maybe :animated selector will help you:

$('#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');
    }
});

Demo

这篇关于检测滚动事件是否在jQuery中手动触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆