滚动特定元素后开始计数

Ali*_*nik 5 html javascript jquery

我创建了一个网站并为我的代码添加了一个计数器.

$(function() {
    function count($this){
        var current = parseInt($this.html(), 10);
        $this.html(++current);
        if(current !== $this.data('count')){
            setTimeout(function(){count($this)}, 50);
        }
    }        
  $(".c-section4").each(function() {
      $(this).data('count', parseInt($(this).html(), 10));
      $(this).html('0');
      count($(this));
  });
});
Run Code Online (Sandbox Code Playgroud)
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<div class="section4">
<div class="section4-bg"></div>

<div class="counter-section">
<span class="c-section4">152 </span>
<h3> ??????? ?? </h3>
</div>

<div class="counter-section">
<span class="c-section4">152 </span>
<h3> ??????? ?? </h3>
</div>

<div class="counter-section">
<span class="c-section4">152 </span>
<h3> ??????? ?? </h3>
</div>

</div>
Run Code Online (Sandbox Code Playgroud)

现在我有一个问题,我想在我滚动到这个元素时反击开始

演示

对不起,我的英语不好

Con*_*Fan 6

您可以处理窗口scroll事件,并使用给定的功能在这里斯科特·道丁,以确定是否该元素已滚动到视图.isCounting可以在每个元素上设置一个标志,以防止同时计数几次.

在以下代码段中,仅在元素可见时执行计数.

$(function () {
    function isScrolledIntoView($elem) {
        var docViewTop = $(window).scrollTop();
        var docViewBottom = docViewTop + $(window).height();
        var elemTop = $elem.offset().top;
        var elemBottom = elemTop + $elem.height();
        return ((elemBottom <= docViewBottom) && (elemTop >= docViewTop));
    }

    function count($this) {
        var current = parseInt($this.html(), 10);
        if (isScrolledIntoView($this) && !$this.data("isCounting") && current < $this.data('count')) {
            $this.html(++current);
            $this.data("isCounting", true);
            setTimeout(function () {
                $this.data("isCounting", false);
                count($this);
            }, 50);
        }
    }

    $(".c-section4").each(function () {
        $(this).data('count', parseInt($(this).html(), 10));
        $(this).html('0');
        $(this).data("isCounting", false);
    });

    function startCount() {
        $(".c-section4").each(function () {
            count($(this));
        });
    };

    $(window).scroll(function () {
        startCount();
    });

    startCount();
});
Run Code Online (Sandbox Code Playgroud)
.tallDiv 
{
   height: 800px;
   background-color: silver;
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>

<div class="section4">
  <div class="section4-bg"></div>
  <div class="tallDiv">Scroll down to test</div>
  <div class="counter-section">
    <span class="c-section4">152 </span>
    <h3> ??????? ?? </h3>
  </div>
  <div class="counter-section">
    <span class="c-section4">152 </span>
    <h3> ??????? ?? </h3>
  </div>
  <div class="counter-section">
    <span class="c-section4">152 </span>
    <h3> ??????? ?? </h3>
  </div>
  <div class="tallDiv"></div>
</div>
Run Code Online (Sandbox Code Playgroud)