【问题标题】:Show decimal value of counter [duplicate]显示计数器的十进制值[重复]
【发布时间】:2020-03-05 15:34:09
【问题描述】:

如何只显示一个计数器的十进制值?目前它的四舍五入到 4.7 到 5

$('.count').each(function() {
  $(this).prop('Counter', 0).animate({
    Counter: jQuery(this).text()
  }, {
    duration: 4000,
    easing: 'swing',
    step: function(now) {
      $(this).text(Math.ceil(now));
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="shiva"><span class="count">200</span></div>
<div id="shiva"><span class="count" id="reducedaily">4.7</span></div>

【问题讨论】:

标签: javascript jquery counter


【解决方案1】:

你可以试试toFixed():

toFixed() 方法使用定点表示法格式化数字。

jQuery('.count').each(function () {
  jQuery(this).prop('Counter',0).animate({
    Counter: jQuery(this).text()
  }, {
      duration: 4000,
      easing: 'swing',
      step: function (now) {
        if(this.id == 'reducedaily'){
          jQuery(this).text(now.toFixed(1));
        }
        else{
          jQuery(this).text(now.toFixed(0));
        }
      }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="shiva"><span class="count">200</span></div>
<div id="shiva2"><span class="count" id="reducedaily">4.7</span></div>

请注意:属性id在文档中必须是唯一的。

【讨论】:

    猜你喜欢
    • 2020-03-21
    • 1970-01-01
    • 2014-05-24
    • 2018-01-28
    • 1970-01-01
    • 2021-10-10
    • 1970-01-01
    • 2015-03-10
    • 2021-02-05
    相关资源
    最近更新 更多