简体   繁体   English

当底部显示100px时如何触发滚动上的jquery函数

[英]how to trigger jquery function on scroll when it shows 100px fro the bottom

I have "fun facts" counter on my web site and also a jQuery function which animate counting numbers inside fun facts div. 我的网站上有“有趣的事实”计数器,还有一个jQuery函数,可以对有趣的事实div中的数字进行动画处理。 Now it is triggered as page loads. 现在,它是在页面加载时触发的。 I would like to start it when user scrolls down to it. 当用户向下滚动时,我想启动它。 For example when div appears 100px from the bottom of the screen. 例如,当div从屏幕底部出现100px时。

This is my jQuery function which starts counting as page loads: 这是我的jQuery函数,从页面加载开始计数:

(function($) {
    "use strict";
    function count($this){
    var current = parseInt($this.html(), 10);
    current = current + 10; /* Where 50 is increment */ 
    $this.html(++current);
        if(current > $this.data('count')){
            $this.html($this.data('count'));
        } else {    
            setTimeout(function(){count($this)}, 50);
        }
    }           
    $(".stat-count").each(function() {
      $(this).data('count', parseInt($(this).html(), 10));
      $(this).html('0');
      count($(this));
    });
})(jQuery);

and here is my try to make it work when user scrolls down to it: 这是我尝试使其在用户向下滚动时起作用的方法:

(function($) {
    "use strict";
    function count($this){
    var current = parseInt($this.html(), 10);
    current = current + 10; /* Where 50 is increment */ 
    $this.html(++current);
        if(current > $this.data('count')){
            $this.html($this.data('count'));
        } else {    
            setTimeout(function(){count($this)}, 50);
        }
    }        




$(window).scroll(function() {
    $(".stat-count").each(function() {


    var imagePos = $(this).offset().top;

    var topOfWindow = $(window).scrollTop();
        if (imagePos < topOfWindow+400) {

          $(this).data('count', parseInt($(this).html(), 10));
          $(this).html('0');
          count($(this));

        }

       }
    }

 })(jQuery);

Thank you! 谢谢! :) :)

May be something like this: 可能是这样的:

$(window).on('scroll', function(event) {
  if(document.body.offsetHeight - event.pageY <= 100 ) {
   //onscoll effect
}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM