如何停止.on("滚动")从射击一旦它的要求得到满足

How to stop the .on("scroll") from firing once its requirements have been met?

本文关键字:quot 满足 on 何停止 滚动 射击      更新时间:2023-09-26

我有以下功能:

当我调用这个函数时,它将div的位置作为参数传递。当保存位置时,如果我从该div的位置向上或向下滚动20像素,则满足要求。满足要求后,如何停止滚动侦听器侦听我的滚动?

如您所见,我已经尝试返回和解除绑定。

      function checkScroll(position) { //Check if user has scroller
        $(document).bind("scroll", function() { //Document on scroll
           if (($(window).scrollTop() < (position - 20)) || ($(window).scrollTop() > (position + 20))) {
              console.log("Requirements met. Stopping scrolling listener now.");
              return; //PREFERABLY, STOP THE SCROLLING EVENT FROM FIRING
              $(document).unbind("scroll"); //Attempt #2
              $(window).unbind("scroll"); // Attempt #3
           }
        });
      }
有人能给我指路吗?谢谢你。

我必须这样做:

删除以下行:

return;

这样它就能到达解绑定的部分。

$(document).unbind("scroll");

代码应该是这样的:

  function checkScroll(position) { //Check if user has scroller
    $(document).bind("scroll", function() { //Document on scroll
       if (($(window).scrollTop() < (position - 20)) || ($(window).scrollTop() > (position + 20))) {
          console.log("Requirements met. Stopping scrolling listener now.");
          $(document).unbind("scroll");
       }
    });
  }