我们有一个动画计数器,可快速计入数字。我们需要在数字上添加一个逗号(成千上万),并且无法弄清楚如何(显然我们是专家)。
我们对柜台的作用非常好:
(function($) {
$.fn.countTo = function(options) {
// merge the default plugin settings with the custom options
options = $.extend({}, $.fn.countTo.defaults, options || {});
// how many times to update the value, and how much to increment the value on each update
var loops = Math.ceil(options.speed / options.refreshInterval),
increment = (options.to - options.from) / loops;
return $(this).each(function() {
var _this = this,
loopCount = 0,
value = options.from,
interval = setInterval(updateTimer, options.refreshInterval);
function updateTimer() {
value += increment;
loopCount++;
$(_this).html(value.toFixed(options.decimals));
if (typeof(options.onUpdate) == 'function') {
options.onUpdate.call(_this, value);
}
if (loopCount >= loops) {
clearInterval(interval);
value = options.to;
if (typeof(options.onComplete) == 'function') {
options.onComplete.call(_this, value);
}
}
}
});
};
$.fn.countTo.defaults = {
from: 0, // the number the element should start at
to: 100, // the number the element should end at
speed: 1000, // how long it should take to count between the target numbers
refreshInterval: 100, // how often the element should be updated
decimals: 0, // the number of decimal places to show
onUpdate: null, // callback method for every time the element is updated,
onComplete: null, // callback method for when the element finishes updating
};
})(jQuery);
jQuery(function($) {
$('.shares').countTo({
from: 2,
to: 1826,
speed: 3000,
refreshInterval: 50,
onComplete: function(value) {
console.debug(this);
}
});
});
有什么建议吗?
答案 0 :(得分:2)
添加:
.replace(/\B(?=(\d{3})+(?!\d))/g, ","));
到
$(_this).html(value.toFixed(options.decimals))
所以你的代码将是这样的:
function updateTimer() {
value += increment;
loopCount++;
$(_this).html(value.toFixed(options.decimals).replace(/\B(?=(\d{3})+(?!\d))/g, ","));
if (typeof(options.onUpdate) == 'function') {
options.onUpdate.call(_this, value);
}
if (loopCount >= loops) {
clearInterval(interval);
value = options.to;
if (typeof(options.onComplete) == 'function') {
options.onComplete.call(_this, value);
}
}
}
答案 1 :(得分:1)
我正在看这篇文章,因为我对同一个jQuery片段有同样的问题。这是我非常简单的解决方案:只需编辑onUpdate
函数即可...
onUpdate: function (value) {
var counter= $("#counter").text();
$("#members").text(counter.substring(0, 3)+","+counter.substring(3, 6));
}
这仅适用于少于1,000,000的数字。否则,您将更改.substring()
参数。
答案 2 :(得分:0)
我没有对此进行测试,但我相信,你会想到这一点,
jQuery.i18n.formatNumber( value, format, [ options ] )Returns: String
因此您需要将您的onUpdateMethod从Null替换为具有类似
的正文的内容control.innerText = jQuery.i18n.formatNumber( value, "n0")
如果要强制执行区域设置,则可以通过传递区域设置
来完成此处提供更多信息 - http://www.jquerysdk.com/api/jQuery.i18n.formatNumber
答案 3 :(得分:0)
我通过添加onUpdate函数解决了这个问题,就像其他人说的那样,但由于我不知道对象,我发现我可以用“this”来设置内部文本。效果很棒!
jQuery(this).find('.milestone-count').delay(6000).countTo({
from: 0,
to: 100000,
speed: 2000,
refreshInterval: 100,
onUpdate: function (value) {
if(this.innerText) // firefox doesn't support innerText
{
this.innerText = newVal;
} else {
this.textContent = newVal;
}
}
});