我想像一个循环那样做,完成后,再次像循环一样启动计数器
样品:
http://jsfiddle.net/4v2wK/226/
// 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(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;
}
最佳答案
function counter(startVal, endVal) {
$({someValue: startVal}).animate({someValue: endVal}, {
duration: 3000,
complete: function() { counter(startVal, endVal) },
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)));
}
});
}
这是一个工作示例:JSFiddle
关于javascript - 计算“循环”之类的数字,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21068334/