【问题标题】:Count to in ViewPort在视口中计数
【发布时间】:2017-02-28 04:19:55
【问题描述】:

我致力于创建一个向上的数字,如以下链接所示: 主题森林链接

但是有很多问题:

第一个问题:当元素显示在视口中时,我需要做动画。

第二个问题:当我再次滚动时,元素编号是从上到下的动画,并且时长效果不好。不知道为什么会出现这个问题。

代码如下:

function countUp() {
    $('.count').each(function () {
        $(this).prop('Counter', 0).animate({
            Counter: $(this).text()
        }, {
            duration: 4000,
            easing: 'swing',
            step: function (now) {
                $(this).text(Math.ceil(now));
            }
        });
    });
}
$(function () {
    "user strict";
    $(window).scroll(function () {
        console.log("scroll top=" + $(this).scrollTop());
        console.log("div offset top=" + $("div").offset().top);
        var scrolling = $(this).scrollTop(),
            divoffset = $(".count").offset().top;
        if (scrolling > divoffset - 300) {
            countUp();
        }
    })
})
body{
    height:4000px;
}
.count{
    width:200px;
    height:200px;
    text-align:center;
    line-height:200px;
    border:1px solid #10f880;
    margin:1000px auto 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="count">1000</div>

注意:我试过这个Stack Overflow suggestion,但我想了解这个想法,谢谢:

【问题讨论】:

    标签: javascript jquery html css count


    【解决方案1】:

    function countUp() {
    $('.count').each(function() {
        $(this).prop('Counter', 0).animate({
          Counter: $(this).text()
        }, {
          duration: 4000,
          easing: 'swing',
          step: function(now) {
            $(this).text(Math.ceil(now));
          }
        });
      });
    }
    $(function() {
      "user strict";
      var bAnimate = true;
      $(".count").css ("opacity", "0.0");
      
      $(window).scroll(function() {
        // console.log("scroll top=" + $(this).scrollTop());
        // console.log("div offset top=" + $("div").offset().top);
        var scrolling = $(this).scrollTop(),
          divoffset = $(".count").offset().top,
          screenBottom = scrolling + $(window).height(), 
          elemBottom = divoffset + $(".count").outerHeight (); // 
        if (screenBottom > elemBottom) {
          if (bAnimate) {
             $(".count").css ("opacity", "1.0");
            countUp();
            bAnimate = false;
          }
        }
      })
    })
    body {
      height: 4000px;
    }
    
    .count {
      width: 200px;
      height: 200px;
      text-align: center;
      line-height: 200px;
      border: 1px solid #10f880;
      margin: 1000px auto 0;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <div class="count">1000</div>

    当屏幕底部(窗口高度)大于元素底部(元素顶部+outerHeight)时执行该函数。 使用 boolean 来阻止函数第二次执行。

    【讨论】:

    • 非常感谢,但是有一个简单的问题,当我按住滚动条时没有执行动画并且我希望用户看到动画,我的意思是当我滚动时我看到最大Number Then The Number 动画
    • 答案已更新以添加该选项。只需使用 opacity 或 display 属性隐藏元素并在执行计数功能之前显示
    猜你喜欢
    • 2016-07-27
    • 1970-01-01
    • 1970-01-01
    • 2015-03-29
    • 2022-12-03
    • 1970-01-01
    • 2020-02-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多