gpt4 book ai didi

javascript - 动画数字计数器不显示小数

转载 作者:行者123 更新时间:2023-11-30 20:27:57 27 4
gpt4 key购买 nike

大家早上好

我有一个从 0 到指定数字的动画数字计数器。但是,如果我输入一个小数(即 99.99%),它会四舍五入为 100%。有没有办法让它计数到给定的数字?

如果您需要更多信息,请告诉我。

提前致谢!

numbersAnimate(_Panel) {

if (!_Panel) {
return;
}

// Get document language
const _Document = document.documentElement;
let lang = _Document.getAttribute('lang');

// Grab all panel amount nodes
const _PanelAmounts = _Panel.querySelectorAll('.panel__amount');
if (!_PanelAmounts) {
return;
}

//Loop over all the panel amounts
for (const _PanelAmount of _PanelAmounts) {

//Convert to jQuery object
const $PanelAmount = $(_PanelAmount);

//Panel amount stored in value
const value = $PanelAmount.data('amount');

//If there is no value return
if (!value || value.length < 1) {
return;
}

// If not a number return
if (isNaN(value)) {
return;
}

//Animation....
$PanelAmount.prop('Counter', 0).animate({
Counter: value
}, {
duration: 1000,
easing: 'linear',
step: function(now) {
$PanelAmount.text(this.Counter.toFixed());
}
});

}

//Add animated class because scroll event checks if it's there, if it isn't then add it.
_Panel.classList.add('animated');





//JSON Structure

"statistic": {
"amount": "99.99",
"symbol": "%"
},
<span className="panel__amount panel-text-style-c" data-amount={jsonData.statistic.amount}>0</span>

最佳答案

您正在使用 Math.ceil(now),它将舍入到最接近的整数。您是否尝试过删除它?

$PanelAmount.text(now).toLocaleString(lang);

关于javascript - 动画数字计数器不显示小数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50678242/

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