jQuery - 滚动功能保持触发:只有一次请 [英] jQuery - Scroll Function Keeps Triggering: Only Once Please

查看:70
本文介绍了jQuery - 滚动功能保持触发:只有一次请的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户水平滚动超过500像素时,我有以下jQuery函数触发 aTestEvent()

I have the following jQuery function which triggers aTestEvent() when the user scrolls horizontally past 500 pixels:

jQuery(document).scroll(function(){
    if(jQuery(this).scrollLeft() >= 500){
           aTestEvent();
 }});

以下是问题:我只想要 aTestEvent()被触发一次!但是,每次用户滚动回到页面的开头然后再次超过500像素时,将再次触发 aTestEvent()

Here's the issue: I only want aTestEvent() to be triggered once! However, every time the user scrolls back to the beginning of the page and then again past 500 pixels, aTestEvent() is triggered again.

我们如何调整上面的代码,以便触发器仅首次出现用户滚动超过500像素?

How can we adjust the above code so that the trigger only occurs for the first time the user scrolls past 500 pixels?

推荐答案

您可以在和上使用 方法:

$(document).on('scroll', function() {
    if( $(this).scrollLeft() >= 500 ) {
        $(document).off('scroll');
        aTestEvent();
    }
});

http://jsfiddle.net/3kacd/

请注意:此代码段可以关闭特定页面上可用的所有滚动事件,但要在不干扰其他滚动处理程序的情况下仅滚动预期的滚动处理程序,我们可以使用命名空间。命名空间类似于CSS类,因为它们不是分层的;只需要匹配一个名称。

Please Note: This code snippet could "off" all the scroll event available on a particular page but to scroll off only intended scroll handler without disturbing other scroll handlers, we can use a namespace. Namespaces are similar to CSS classes in that they are not hierarchical; only one name needs to match.

$(document).on('name1.scroll', function() {
    if( $(this).scrollLeft() >= 500 ) {
        $(document).off('name1.scroll');
        aTestEvent();
    }
});

http://jsfiddle.net/shekhardtu/3kacd/57/

http://jsfiddle.net/shekhardtu/3kacd/57/

这篇关于jQuery - 滚动功能保持触发:只有一次请的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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