我有這個代碼完美計數,但客戶希望它從16.5到9.99計數。我相信可以讓結尾的數字包含兩個十進制數字。作爲代碼底部的選項,您會注意到有一個十進制選項。也許在計數的一半,我可以將它從1位小數改爲2位小數?我怎樣才能做到這一點?謝謝!用小數點數jquery
(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: 1, // 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($) {
$('#heroCounter').countTo({
from: 16.5,
to: 9.9,
speed: 1500,
refreshInterval: 50,
onComplete: function(value) {
console.debug(this);
}
});
});
這工作完美。非常感謝您的幫助 – jdoimeadios23
歡迎您:) –