Unbind a specific function jQuery

帅比萌擦擦* 提交于 2019-12-04 13:12:08

问题


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");});
     }
 });

回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/15106918/unbind-a-specific-function-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!