gpt4 book ai didi

javascript - jQuery Counter, animation to a total,但我如何包含一个逗号?

转载 作者:行者123 更新时间:2023-11-30 18:16:31 25 4
gpt4 key购买 nike

我在这里为一个计数器找到了许多解决方案,从一个总数到另一个总数的动画。

这是我现在使用的:

jQuery.fn.extend({
ts : function (from, to, time) {
var steps = 1,
self = this,
counter;

if (from - to > 0) {
steps = -1;
};

from -= steps;

function step() {
self.text(from += steps);

if ((steps < 0 && to >= from) || (steps > 0 && from >= to)) {
clearInterval(counter);
};
};

counter = setInterval(step, time || 5);
}
});


var total = $('.total').ts(56000,56941);

效果很好。但是,我想在总数中添加一个逗号,例如 56,941。这可能吗?

最佳答案

我想这样就可以了:

jQuery.fn.extend({
ts: function(from, to, time) {
var steps = 1, self = this, counter;

if (from - to > 0) steps = -1;
from -= steps;

function step() {
var x = (from += steps).toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
self.text(x);
if ((steps < 0 && to >= from) || (steps > 0 && from >= to)) {
clearInterval(counter);
};
};
counter = setInterval(step, time || 5);
}
});

FIDDLE

关于javascript - jQuery Counter, animation to a total,但我如何包含一个逗号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13131444/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com