简体   繁体   English

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

[英]Detect jquery event trigger by user or call by code

I have some window.onscroll event 我有一些window.onscroll事件

$(window).scroll(function(e){
    //My Stuff
});

but in my code I call animate scroll to some where 但在我的代码中,我将动画滚动调用到某些地方

$('html, body').stop().animate({
   scrollTop:555
}, 1000);

so how I detect the page was scroll by user or call by my code. 所以我如何检测页面是由用户滚动或由我的代码调用。 My current solution is put a flag before call animate in my code then clear it but it's not clever solution. 我的当前解决方案是在我的代码中调用animate之前放置一个标志,然后清除它,但这不是一个聪明的解决方案。 I've also read about detect e.which or e.originalEvent but it's not work. 我也读过关于检测e.whiche.originalEvent但它不起作用。 I think you expert have a good solution here. 我想你的专家在这里有一个很好的解决方案。

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

$('#scroller').scroll(); // just a initial call

When you scroll by call the e.originalEvent will undefined but when scroll manually it will give scroll object. 当你通过调用滚动时, e.originalEvent将是undefined但是当手动滚动时它将给出scroll对象。

DEMO DEMO

ive reasked this question and got 2 helpful answers. 我已经重新提出了这个问题,得到了2个有用的答案。
i'll link the question here for others who'll find this thread. 我会在这里链接这个问题给其他人找到这个帖子。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM