Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 802
o01 Avatar asked Nov 11 '10 13:11

o01


1 Answers

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

like image 107
alex Avatar answered Sep 18 '22 00:09

alex