【问题标题】:Only fire a function once on scroll (scrollstop)仅在滚动时触发一次功能(scrollstop)
【发布时间】:2013-06-01 13:43:45
【问题描述】:

所以,我只想在滚动时触发一次函数(使用 Scrollstop,由 stackoverflow 答案给出)

问题是我不能只触发一次函数。我尝试了不同的解决方案(.on()、设置计数器、将其设置在 window.scrollstop 函数的外部/内部)但没有任何效果。

我不认为这很困难,但是..到目前为止我还没有成功。

这是我正在使用的插件

  $.fn.scrollStopped = function(callback) {           
          $(this).scroll(function(){
              var self = this, $this = $(self);
              if ($this.data('scrollTimeout')) {
                clearTimeout($this.data('scrollTimeout'));
              }
              $this.data('scrollTimeout', setTimeout(callback,300,self));
          });
      };

这是我的代码:

        $(window).scrollStopped(function(){
            if ($(".drawing1").withinViewport()) {      
                doNothing()
                }
            })


var doNothing = function() {
            $('#drawing1').lazylinepainter('paint');
        }

(由于计数器不起作用,因此删除了计数器)

Live demo here

PS:我只想实现一次的功能是lazyPaint。它在我们滚动到元素时开始,但在结束时再次触发。

【问题讨论】:

  • 所以你的函数 doNothing() 实际上做了什么?
  • @roasted 是的,愚蠢的函数名,我知道:X

标签: javascript jquery jquery-plugins


【解决方案1】:

如何使用变量来查看它之前是否被触发:

var fired = 0;
$.fn.scrollStopped = function(callback) {           
          $(this).scroll(function(){
              if(fired == 0){
                var self = this, $this = $(self);
                if ($this.data('scrollTimeout')) {
                  clearTimeout($this.data('scrollTimeout'));
                }
                $this.data('scrollTimeout', setTimeout(callback,300,self));
                fired = 1;
              }
          });
      };

【讨论】:

    【解决方案2】:

    这是我在监听滚动事件时触发一次函数的版本:

    var fired = false;
    window.addEventListener("scroll", function(){
      if (document.body.scrollTop >= 1000 && fired === false) {
        alert('This will happen only once');
        fired = true;
      }
    }, true)
    

    【讨论】:

      【解决方案3】:

      这些 anwsers 对我不起作用,所以这是我的代码:

              var fired = 0;
              jQuery(this).scroll(function(){
                  if(fired == 0){
                      alert("fired");
                      fired = 1;
                  }
              });
      

      【讨论】:

        【解决方案4】:

        这个解决方案怎么样?

        function scrollEvent() {
          var hT = $('#scroll-to').offset().top,
            hH = $('#scroll-to').outerHeight(),
            wH = $(window).height(),
            wS = $(this).scrollTop();
          if (wS > (hT+hH-wH)){
            console.log('H1 on the view!');
            window.removeEventListener("scroll", scrollEvent);
          }
        }
        window.addEventListener("scroll", scrollEvent);
        

        【讨论】:

        • 虽然此代码 sn-p 可能是解决方案,但 including an explanation 确实有助于提高您的帖子质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。
        猜你喜欢
        • 1970-01-01
        • 2016-04-21
        • 2016-09-11
        • 2018-07-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多