最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Adding thousand separator to jQuery Counter - Stack Overflow

programmeradmin3浏览0评论

I am trying to add mas (thousand separator) for big numbers that use the follow counter function:

<script type='text/javascript'>
    $(window).load(function() {
        $('.Count').each(function() {
            var $this = $(this);
            jQuery({Counter: 0}).animate({Counter: $this.text()}, {
                duration: 1500,
                easing: 'swing',
                step: function() {
                    $this.text(Math.ceil(this.Counter));
                }
            });
        });
    });

Do I modify this particular formula or do I have to write an additional function that will handle the formatting?

I am trying to add mas (thousand separator) for big numbers that use the follow counter function:

<script type='text/javascript'>
    $(window).load(function() {
        $('.Count').each(function() {
            var $this = $(this);
            jQuery({Counter: 0}).animate({Counter: $this.text()}, {
                duration: 1500,
                easing: 'swing',
                step: function() {
                    $this.text(Math.ceil(this.Counter));
                }
            });
        });
    });

Do I modify this particular formula or do I have to write an additional function that will handle the formatting?

Share Improve this question edited Dec 8, 2014 at 20:48 user3088202 asked Dec 8, 2014 at 20:33 user3088202user3088202 3,1747 gold badges29 silver badges39 bronze badges 2
  • so what's the problem that you want to get solved? – Chanckjh Commented Dec 8, 2014 at 20:43
  • I am trying to find a way to have my counter animation run on large numbers that use that use mas. – user3088202 Commented Dec 8, 2014 at 20:51
Add a ment  | 

2 Answers 2

Reset to default 7

I had the same issue with large numbers inconsistently ending (ie. 550,000 was sometimes only counting as far as 549,826 or so).

Here's a simplified solution which toLocaleString function to add the decimal accordingly:

$('.js-count').each(function() {
   
   $(this).prop('Counter', 0).animate({
      
      Counter: $(this).data('number')
   }, {
      
      duration: 4000,
      easing: 'swing',
      step: function(now) {
         
         $(this).text(Math.ceil(now).toLocaleString('en'));
      }
   });
  
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class="js-count" data-number="550000"></span>

I've also opted to use a data attribute to store the number. This way is a bit more bullet-proof. Relying on the content of the element can lead to miscalculating if the event fires more than once.

this might work

<script>
$(window).load(function() {
    $('.Count').each(function() {
        var $this = $(this);
        jQuery({Counter: 0}).animate({Counter: $this.text()}, {
            duration: 1500,
            easing: 'swing',
            step: function() {
                var num = Math.ceil(this.Counter).toString();
                if(Number(num) > 999){
                    while (/(\d+)(\d{3})/.test(num)) {
                        num = num.replace(/(\d+)(\d{3})/, '$1' + ',' + '$2');
                    }
                }
                $this.text(num);
            }
        });
    });
});
</script>
发布评论

评论列表(0)

  1. 暂无评论