Animated Counter on scroll [duplicate]

问题: This question already has an answer here: How to print a number with commas as thousands separators in JavaScript 43 answers So, I found this cool...

问题:

This question already has an answer here:

So, I found this cool pen Creative Animated Counter on Scroll but I want to know how to format the numbers so they have a comma if it's over a thousand.

function visible(partial) {
  var $t = partial,
    $w = jQuery(window),
    viewTop = $w.scrollTop(),
    viewBottom = viewTop + $w.height(),
    _top = $t.offset().top,
    _bottom = _top + $t.height(),
    compareTop = partial === true ? _bottom : _top,
    compareBottom = partial === true ? _top : _bottom;

  return ((compareBottom <= viewBottom) && (compareTop >= viewTop) && $t.is(':visible'));
}

$(window).scroll(function() {
  if (visible($('.count-digit'))) {
    if ($('.count-digit').hasClass('counter-loaded')) return;
    $('.count-digit').addClass('counter-loaded');

    $('.count-digit').each(function() {
      var $this = $(this);
      jQuery({
        Counter: 0
      }).animate({
        Counter: $this.text()
      }, {
        duration: 5000,
        easing: 'swing',
        step: function() {
          $this.text(Math.ceil(this.Counter));
        }
      });
    });
  }
})

回答1:

The method you're looking for is toLocaleString. You can invoke this on a number to get the commas you're looking for. You would just need to change your step function like so:

step: function() {
  $this.text(Math.ceil(this.Counter).toLocaleString());
}
  • 发表于 2018-09-02 12:23
  • 阅读 ( 267 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除