Unbind a specific function jQuery
Asked Answered
S

2

7

What I'm trying to do is unbind a specific function, after it has run once. In the code below it's the window scroll.

 $(window).scroll(function(){
     if($(window).scrollTop() == viewportheight ){
          $("#landing_page").fadeOut(function() { $(window).scrollTop(0); $(window).unbind("scroll");});
     }
 });

Basicly, when the #div fades out, I want it to scrollTop(0). After scrolling top, I need this entire function to unbind.

Is there a way to give this function a specific name, and then call back that name? Because this code works, only it removes all scroll functions. (Wich ofcourse I don't want) I was thinking something like so:

 $(window).scroll(function(FUNCTION NAME HERE){
     if($(window).scrollTop() == viewportheight ){
          $("#landing_page").fadeOut(function() { $(window).scrollTop(0); $(window).unbind("FUNCTION NAME HERE");});
     }
 });
Swaggering answered 27/2, 2013 at 8:2 Comment(0)
C
27

Jefferson, something like...

$(window).bind("scroll.myScroll", function(){
     // stuff
  })

now, unbind the scroll - I have it a unique identifier just in case you have other window scroll events, you don't want to mess with.

$(window).unbind('.myScroll');
Chaddy answered 27/2, 2013 at 8:7 Comment(1)
Is there posibility to unbind a function created like this: jQuery.fn.scrollVal = function (jump, type) {...}? Becuase sth like this doesn't work: $('#ReleaseVersion').unbind('.scrollVal');Chancellorship
V
0

you can use jquery.unbind function to unbind any particular event for the element

Vamoose answered 27/2, 2013 at 8:4 Comment(1)
I've tried unbinding the scrollTop, wich doesn't work. It keeps firing the function.Swaggering

© 2022 - 2024 — McMap. All rights reserved.