【问题标题】:Countup.js final number does not have commaCountup.js 最终数字没有逗号
【发布时间】:2020-01-12 21:36:53
【问题描述】:

我是 javascript 菜鸟,毫无疑问。

我的屏幕上有 JQuery CountUp,并浏览了其中一些论坛。计数器在计数时显示一个逗号,但最终数字 (102,050) 仍然没有显示逗号 (102050)

任何指针?这是现在的代码...

$('.counter').each(function() {
  const $this   = $(this) 
  const countTo = $this.attr('data-count')

  $({ countNum: $this.text()}).animate({ countNum: countTo },
    {
      duration: 1500,
      easing:'linear',
      step: function() {
        $this.text(Math.floor(this.countNum).toLocaleString('en'));
      },
      complete: function (now) {
        now = Number(Math.ceil(now)).toLocaleString('en');
        $(this).text(now);
        $this.text(this.countNum).toLocaleString('en');
      }
    }
  );
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="counter" data-count="102050">1</div>

【问题讨论】:

    标签: javascript jquery counter


    【解决方案1】:

    只需拨打.toLocaleString('en') this.countNum 而不是$this.text(...)
    另外你真的只需要complete回调中的一串代码

    $('.counter').each(function() {
      const $this   = $(this) 
      const countTo = $this.attr('data-count')
    
      $({ countNum: $this.text()}).animate({ countNum: countTo },
        {
          duration: 3500,
          easing:'linear',
          step: function() {
            $this.text(Math.floor(this.countNum).toLocaleString('en'));
          },
          complete: function() {
            $this.text(this.countNum.toLocaleString('en'));
          }
        }
      );
    })
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div class="counter" data-count="102050">1</div>

    【讨论】:

      猜你喜欢
      • 2014-09-14
      • 2017-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-08
      • 1970-01-01
      相关资源
      最近更新 更多