【问题标题】:jQuery number counterjQuery数字计数器
【发布时间】:2016-04-11 18:32:32
【问题描述】:

我正在使用这个 jQuery sn-p 动画一个从 0 到跨度中提供的数字的数字计数器。有人可以告诉我如何修改它以在从给定值开始时将数字倒数到 0 吗?

<span class="count">200</span>

$('.count').each(function () {
    $(this).prop('Counter', 0).animate({
        Counter: $(this).text()
    }, {
        duration: 4000,
        easing: 'swing',
        step: function (now) {
            $(this).text(Math.ceil(now));
        }
    });
});

【问题讨论】:

标签: javascript jquery html


【解决方案1】:

这样的?我假设 step 函数中的“现在”只是计算步数。

$('.count').each(function () {
    $(this).data('start', parseInt($(this).text())).animate({
        Counter: $(this).text()
    }, {
        duration: 4000,
        easing: 'swing',
        step: function (now) {
            $(this).text($(this).data(start) - now);
        }
    });
});

我在 jsFiddle (https://jsfiddle.net/0mtht3xm/) 中验证了此功能:

$('.count').each(function () {
    $(this).data('start', parseInt($(this).text())).prop('Counter', 0).animate({
        Counter: $(this).text()
    }, {
        duration: 4000,
        easing: 'swing',
        step: function (now) {
            $(this).text($(this).data('start') - Math.ceil(now));
        }
    });
});

【讨论】:

    【解决方案2】:

    有无数种不同的方法可以创建一个倒计时到 0 的计数器。这就是我的做法:

    HTML:

    <span class="count">200</span>
    

    JS:

    var $count = $('.count');
    
    var num = parseInt($count.text());
    var interval = window.setInterval(updateTime, 1000);
    
    function updateTime() {
      num--;
      $count.text(num.toString());
      if (num <= 0) {
        window.clearInterval(interval);
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-02-02
      • 2021-09-04
      • 1970-01-01
      • 1970-01-01
      • 2018-11-07
      • 1970-01-01
      • 2012-10-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多