48

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.

o01
  • 4,178
  • 10
  • 36
  • 69
  • 1
    Have you tried to unbind the scroll-event explicitly (via `.unbind("scroll")`)? – jwueller Nov 11 '10 at 13:40
  • 1
    Does it stop if you just call `block_scroll()`? Otherwise, try adding `$('html, body').stop();` to the `else` branch after `unbind`. – stealthyninja Nov 11 '10 at 13:47
  • Please, consider to use namespaced events, otherwise you will unbind all scroll events, even those defined by other libraries. –  Nov 11 '10 at 13:47
  • You could post the answer to this yourself and accept it. This will make it easier to understand for other visitors. – jwueller Nov 11 '10 at 13:51

6 Answers6

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

Community
  • 1
  • 1
alex
  • 438,662
  • 188
  • 837
  • 957
19

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');
Rory McCrossan
  • 306,214
  • 37
  • 269
  • 303
  • 1
    Thank you for providing an update to an old question! This definitely is the more accurate approach to date. – David Jun 16 '19 at 03:15
2

Try this instead

$.unbind('scroll');

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

pestaa
  • 4,723
  • 2
  • 20
  • 32
  • 4
    That example isn't correct usage. It's a method to act on a jQuery collection, not a *static* method. – alex Dec 11 '11 at 11:14
1

try this:

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

it works in my project

DinoMyte
  • 8,367
  • 1
  • 15
  • 23
王大力
  • 11
  • 1
1

You need to:

unbind('scroll')

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

Coin_op
  • 9,871
  • 4
  • 31
  • 44
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);
hsuk
  • 6,321
  • 12
  • 45
  • 78
Olavxxx
  • 196
  • 10
  • 1
    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;`. – HelpNeeder Jan 21 '15 at 18:35
  • 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). – Olavxxx Jan 22 '15 at 12:19