2010-11-29 15 views
27

मैं कैसे निकाल सकते हैं और उसके बाद $(window).scroll जोड़ सकता हूँ? मैं एक चर की दुकान और कुछ घटना के बाद यह पुन: उपयोग करने की जरूरत है।

// here i store my var 
$(window).scroll(function(){ 
    myScroll = $(window).scrollTop() 
}); 

$("#itemUnbind").click(function(){ 
    // here i need to remove the listener   
}); 

$("#itemBind").click(function(){ 
    // here i need to add listener again  
}); 

धन्यवाद ।

उत्तर

63

आप एक चर में समारोह की दुकान और फिर off का उपयोग इसे हटाने के लिए की जरूरत है:

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

धन्यवाद ... यह – Dee

+1

@Dee मेरे लिए ठीक है - अगर यह आपके लिए काम करता है, यह चिह्नित करें जैसा "टिक" आइकन पर क्लिक करके उत्तर। – Fenton

+3

मुझे लगता है कि यह होना चाहिए: $ ("# itemUnbind")। क्लिक करें (फ़ंक्शन() { $ (विंडो) .unbind ("स्क्रॉल", स्क्रॉलहैंडलर); }); –

संबंधित मुद्दे