如何使用jQuery使用逗号使用动画来增加数字?

我正在尝试在页面上的元素内增加一个数字。 但是我需要用数字来包含第千位值的逗号。 (例如45,000而不是45000)

 // Animate the element's value from x to y: $({someValue: 40000}).animate({someValue: 45000}, { duration: 3000, easing:'swing', // can be anything step: function() { // called on every step // Update the element's text with rounded-up value: $('#el').text(Math.round(this.someValue)); } });  

如何使用动画使用逗号增加数字?

工作演示 http://jsfiddle.net/4v2wK/

随意更改它以满足您的需求,您还可以查看货币格式化程序,希望这将符合您的需求:)

 // Animate the element's value from x to y: var $el = $("#el"); //[make sure this is a unique variable name] $({someValue: 40000}).animate({someValue: 45000}, { duration: 3000, easing:'swing', // can be anything step: function() { // called on every step // Update the element's text with rounded-up value: $el.text(commaSeparateNumber(Math.round(this.someValue))); } }); function commaSeparateNumber(val){ while (/(\d+)(\d{3})/.test(val.toString())){ val = val.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,"); } return val; } 

**输出* 在此处输入图像描述

您还应该添加一个完整的函数:

 step:function(){ //.. }, complete:function(){ $el.text(commaSeparateNumber(Math.round(this.someValue))); } 

更新小提琴: 演示

Interesting Posts