如何检查滚动绑定是否已添加到窗口?

时间:2014-03-16 11:26:52

标签: javascript jquery javascript-events

我想检查是否在jquery中将scroll事件绑定到window元素。基本上我想看看有人做过这样的事情:

$(window).scroll(function() { ... });

在寻找SO之后,我找到了更常见的问题,例如if event already exist on an element和一个有效的解决方案。

但是当我尝试使用$.data( $(window).get(0), 'events' )时,我得到undefined。提出了类似的解决方案here

那么检查滚动事件的正确方法是什么?

1 个答案:

答案 0 :(得分:2)

我想这就是你想要的:

$._data(window).events.scroll

如果没有此类事件,则返回undefined绑定到window对象

示例:

JSFiddle

alert($._data(window).events.scroll); // Should return 'undefined'

$(window).scroll(function() {
    alert('a');
});

alert($._data(window).events.scroll); // Should return 1 object

$(window).scroll(function() {
    alert('b');
});

alert($._data(window).events.scroll); // Should return 2 objects