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

6

50

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.

Burrus answered 11/11, 2010 at 13:36 Comment(4)
Have you tried to unbind the scroll-event explicitly (via .unbind("scroll"))?Synergist
Does it stop if you just call block_scroll()? Otherwise, try adding $('html, body').stop(); to the else branch after unbind.Beaty
Please, consider to use namespaced events, otherwise you will unbind all scroll events, even those defined by other libraries.Sharkey
You could post the answer to this yourself and accept it. This will make it easier to understand for other visitors.Synergist
M
75
$(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).

Molloy answered 11/11, 2010 at 13:39 Comment(4)
@pestaa If it makes you feel better, I've hit my rep cap :PMolloy
Tried this. Doesn't unbind :(Burrus
@Molloy It actually makes me feel worse. :DTestify
@alex: Your update is right. JavaScript does not make a difference between single and double quotes. This is probably a browser-specific problem. @Orolin: What is your browser?Synergist
S
21

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');
Spang answered 27/3, 2017 at 15:29 Comment(1)
Thank you for providing an update to an old question! This definitely is the more accurate approach to date.Antilogy
T
2

Try this instead

$.unbind('scroll');

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

Testify answered 11/11, 2010 at 13:40 Comment(1)
That example isn't correct usage. It's a method to act on a jQuery collection, not a static method.Molloy
B
2

try this:

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

it works in my project

Bouse answered 27/2, 2013 at 7:12 Comment(0)
A
1

You need to:

unbind('scroll')

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

Acting answered 11/11, 2010 at 13:40 Comment(0)
O
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);
Obed answered 24/4, 2013 at 8:21 Comment(2)
I don't understand the usage of this. First, what #main should point to? Also, never used $j(). You mean the scope? So this mean you should reveal the scope like this: window.jQuery = window.$j = jQuery;.Privy
I posted this in 2013, but I think I misunderstood the question (when looking at it now in 2015). My code is for scrolling to top, the #main is an element with an id, but it could be a banner, or a menu or something... The $j is an initialization of jquery with noconflct. (can be what ever one wants).Obed

© 2022 - 2024 — McMap. All rights reserved.