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

120,323

Solution 1

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

Solution 2

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

Solution 3

Try this instead

$.unbind('scroll');

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

Solution 4

try this:

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

it works in my project

Solution 5

You need to:

unbind('scroll')

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

Share:
120,323
o01
Author by

o01

I hate Webpack. I also hate Babel, and everything else in the unavoidable configuration nightmare that comes with being a Javascript developer. As of July 2020 I hate everyone involved in the design of how CORS and cookies "function". Mongoose has the worst docs and is just awful in general. I mean, WTF? And this bullshit!? express-session can kiss my ass.

Updated on March 28, 2020

Comments

  • o01
    o01 about 4 years

    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.

  • user3167101
    user3167101 over 13 years
    @pestaa If it makes you feel better, I've hit my rep cap :P
  • o01
    o01 over 13 years
    Tried this. Doesn't unbind :(
  • pestaa
    pestaa over 13 years
    @alex It actually makes me feel worse. :D
  • jwueller
    jwueller over 13 years
    @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?
  • user3167101
    user3167101 over 12 years
    That example isn't correct usage. It's a method to act on a jQuery collection, not a static method.
  • HelpNeeder
    HelpNeeder over 9 years
    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;.
  • Olavxxx
    Olavxxx over 9 years
    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).
  • David
    David almost 5 years
    Thank you for providing an update to an old question! This definitely is the more accurate approach to date.