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

发布于 2024-10-02 06:44:36 字数 587 浏览 5 评论 0原文

我有这个功能:

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

第一部分按其应有的方式工作,但是当我稍后调用 block_scroll(false) 时 - 它仍然处于阻塞状态。做什么?

重新编辑 因此,按照建议,我尝试了......

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

但有些困惑。起初它不起作用 - 然后它起作用了。

现在我认为它失败了,因为我在调用 block_scroll(false) 时正在滚动。我现在已经测试过好几次了。是的,如果我在脚本运行时什么都不做并且调用 block_scroll(false) - 它确实有效。但如果我在调用它时滚动,则不会。

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?

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

囍孤女 2024-10-09 06:44:36
$(window).unbind('scroll');

尽管文档说如果不带参数调用它将删除所有事件处理程序,但值得给出一个尝试显式解除绑定。

更新

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

$(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).

弄潮 2024-10-09 06:44:36

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

从 jQuery 3.0 开始,.unbind() 已被弃用。自 jQuery 1.7 起,它被 .off() 方法取代,因此不鼓励使用它。

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

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

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.

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.

Instead, you should now use off():

$(window).off('scroll');
停滞 2024-10-09 06:44:36

试试这个

$.unbind('scroll');

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

Try this instead

$.unbind('scroll');

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

你列表最软的妹 2024-10-09 06:44:36

试试这个:

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

它适用于我的项目

try this:

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

it works in my project

节枝 2024-10-09 06:44:36

您需要:

unbind('scroll')

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

You need to:

unbind('scroll')

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

活泼老夫 2024-10-09 06:44:36

非常老的问题,但如果其他人偶然发现它,我建议尝试:

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文