$(窗口)上的jQuery绑定/取消绑定'scroll'事件

时间:2022-08-26 09:14:08

I have this function:

我有这个功能:

function block_scroll(key){
    if (key) {
        $(window).bind("scroll", function(){
            $('html, body').animate({scrollTop:0}, 'fast');
        });
    } else {
        $(window).unbind();
    }
}

The first part works as it should, but when I later call block_scroll(false) - it's still blocking. Wat do?

第一部分应该工作,但是当我稍后调用block_scroll(false)时 - 它仍然阻塞。笏呢?

RE-EDIT So as suggested I tried...

重新编辑所以我试过......

$(window).unbind("scroll");

...with some confusion. At first it didn't work - then it worked.

......有些困惑。起初它没有用 - 然后它起作用了。

Now I think it failed because I was scrolling the moment block_scroll(false) was called. I've tested this several times now. And yes, if I do nothing while the script runs and block_scroll(false) is called - it does work. But it doesn't if I'm scrolling when it's called.

现在我觉得它失败了,因为我在滚动的时候调用了block_scroll(false)。我现在已经多次测试了这个。是的,如果我在脚本运行时没有做任何事情并且调用了block_scroll(false) - 它确实有效。但是,如果我在调用它时滚动它就不会。

6 个解决方案

#1


63  

$(window).unbind('scroll');

Even though the documentation says it will remove all event handlers if called with no arguments, it is worth giving a try explicitly unbinding it.

尽管文档说如果在没有参数的情况下调用它将删除所有事件处理程序,但值得尝试显式解除绑定。

Update

It worked if you used single quotes? That doesn't sound right - as far as I know, JavaScript treats single and double quotes the same (unlike some other languages like PHP and C).

如果您使用单引号,它会起作用吗?这听起来不对 - 据我所知,JavaScript对单引号和双引号的处理方式相同(与PHP和C等其他语言不同)。

#2


8  

Note that the answers that suggest using unbind() are now out of date as that method has been deprecated and will be removed in future versions of jQuery.

请注意,建议使用unbind()的答案现已过时,因为该方法已被弃用,并将在未来的jQuery版本中删除。

As of jQuery 3.0, .unbind() has been deprecated. It was superseded by the .off() method since jQuery 1.7, so its use was already discouraged.

从jQuery 3.0开始,.unbind()已被弃用。自jQuery 1.7以来,它被.off()方法取代,所以它的使用已经不再使用了。

Instead, you should now use off():

相反,你现在应该使用off():

$(window).off('scroll');

#3


2  

Try this instead

试试这个

$.unbind('scroll');

http://api.jquery.com/unbind/

http://api.jquery.com/unbind/

#4


1  

You need to:

你需要:

unbind('scroll')

At the moment you are not specifying the event to unbind.

目前您没有指定要取消绑定的事件。

#5


0  

Very old question, but in case someone else stumbles across it, I would recommend trying:

很老的问题,但万一其他人偶然发现它,我建议尝试:

$j("html, body").stop(true, true).animate({
        scrollTop: $j('#main').offset().top 
}, 300);

#6


0  

try this:

尝试这个:

$(window).unbind('scroll');

it works in my project

它适用于我的项目

#1


63  

$(window).unbind('scroll');

Even though the documentation says it will remove all event handlers if called with no arguments, it is worth giving a try explicitly unbinding it.

尽管文档说如果在没有参数的情况下调用它将删除所有事件处理程序,但值得尝试显式解除绑定。

Update

It worked if you used single quotes? That doesn't sound right - as far as I know, JavaScript treats single and double quotes the same (unlike some other languages like PHP and C).

如果您使用单引号,它会起作用吗?这听起来不对 - 据我所知,JavaScript对单引号和双引号的处理方式相同(与PHP和C等其他语言不同)。

#2


8  

Note that the answers that suggest using unbind() are now out of date as that method has been deprecated and will be removed in future versions of jQuery.

请注意,建议使用unbind()的答案现已过时,因为该方法已被弃用,并将在未来的jQuery版本中删除。

As of jQuery 3.0, .unbind() has been deprecated. It was superseded by the .off() method since jQuery 1.7, so its use was already discouraged.

从jQuery 3.0开始,.unbind()已被弃用。自jQuery 1.7以来,它被.off()方法取代,所以它的使用已经不再使用了。

Instead, you should now use off():

相反,你现在应该使用off():

$(window).off('scroll');

#3


2  

Try this instead

试试这个

$.unbind('scroll');

http://api.jquery.com/unbind/

http://api.jquery.com/unbind/

#4


1  

You need to:

你需要:

unbind('scroll')

At the moment you are not specifying the event to unbind.

目前您没有指定要取消绑定的事件。

#5


0  

Very old question, but in case someone else stumbles across it, I would recommend trying:

很老的问题,但万一其他人偶然发现它,我建议尝试:

$j("html, body").stop(true, true).animate({
        scrollTop: $j('#main').offset().top 
}, 300);

#6


0  

try this:

尝试这个:

$(window).unbind('scroll');

it works in my project

它适用于我的项目