jQuery 绑定/取消绑定 $(window) 上的“滚动”事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4154952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:45:50  来源:igfitidea点击:

jQuery bind/unbind 'scroll' event on $(window)

jquerybindunbind

提问by o01

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-EDITSo 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) - 它确实有效。但是,如果我在调用它时滚动,则不会。

回答by alex

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

Even though the documentationsays 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)。

回答by Rory McCrossan

Note that the answers that suggest using unbind()are now out of date as that method has been deprecatedand 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');

回答by pestaa

Try this instead

试试这个

$.unbind('scroll');

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

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

回答by Coin_op

You need to:

你需要:

unbind('scroll')

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

目前您没有指定要解除绑定的事件。

回答by u1249638

try this:

尝试这个:

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

it works in my project

它适用于我的项目

回答by Olavxxx

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);