检测用户的jquery事件触发器或代码调用 [英] Detect jquery event trigger by user or call by code

查看:67
本文介绍了检测用户的jquery事件触发器或代码调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些 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);

所以我如何检测页面是由用户滚动还是由我的代码调用。我的当前解决方案是在我的代码中调用 animate 之前放置一个标志,然后清除它,但这不是一个聪明的解决方案。我还读过关于检测 e.which e.originalEvent 但它不起作用。我认为你的专家在这里有一个很好的解决方案。

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. I've also read about detect e.which or e.originalEvent but it's not work. 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

当您通过调用滚动 e.originalEvent时未定义但手动滚动时会给出滚动对象。

When you scroll by call the e.originalEvent will undefined but when scroll manually it will give scroll object.

DEMO

DEMO

这篇关于检测用户的jquery事件触发器或代码调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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