jQuery add and remove $(window).scroll(function()?

后端 未结 1 1566
暖寄归人
暖寄归人 2021-02-05 01:21

How can I remove and then add the $(window).scroll? I need to store a variable and reuse it after some event.

// here i store my var
$(window).scrol         


        
相关标签:
1条回答
  • 2021-02-05 02:02

    You need to store the function in a variable and then use off to remove it:

    var scrollHandler = function(){
        myScroll = $(window).scrollTop();
    }
    
    $("#itemBind").click(function(){
        $(window).scroll(scrollHandler);
    }).click(); // .click() will execute this handler immediately
    
    $("#itemUnbind").click(function(){
        $(window).off("scroll", scrollHandler);
    });
    
    0 讨论(0)
提交回复
热议问题