【问题标题】:Animated Counter on scroll [duplicate]滚动上的动画计数器[重复]
【发布时间】:2018-08-29 15:08:02
【问题描述】:

所以,我找到了这支很酷的笔Creative Animated Counter on Scroll,但我想知道如何格式化数字,以便在超过一千个时使用逗号。

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));
        }
      });
    });
  }
})

【问题讨论】:

  • 使用提供的重复问题,您可以使用已接受的答案并更改以下行:$this.text(Math.ceil(this.Counter));。希望对您有所帮助!
  • 试试这个 $this.text(Math.ceil(this.Counter).toString().replace(/\B(?=(\d{3})+(?!\d) )/g, ","));

标签: javascript jquery comma


【解决方案1】:

您要查找的方法是toLocaleString。您可以在数字上调用它来获取您要查找的逗号。您只需要像这样更改步进函数:

step: function() {
  $this.text(Math.ceil(this.Counter).toLocaleString());
}

【讨论】:

  • 那行得通。非常感谢!
猜你喜欢
  • 2013-04-04
  • 2012-11-12
  • 1970-01-01
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多