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).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 });
Thank you.
When the mouse won't scroll, there are two issues that most commonly cause it. The first is dust and dirt causing mechanical issues with the mouse wheel. The second is low battery issues on wireless mice.
jQuery scroll() MethodThe scroll event occurs when the user scrolls in the specified element. The scroll event works for all scrollable elements and the window object (browser window). The scroll() method triggers the scroll event, or attaches a function to run when a scroll event occurs.
$(document). ready(function(){ var scrollPos = 0; var Counter = 0; $(window). scroll(function(){ var scrollPosCur = $(this). scrollTop(); if (scrollPosCur > scrollPos) { Counter -= 1; } else { Counter += 1; } scrollPos = scrollPosCur; }); });
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); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With