正在努力解决这个问题,所以非常感谢任何想法或建议。
我有一个计算(工作),计算自2021年1月1日以来已经过去了多少天,并通过一个值*。我想动画从0到值在3秒的时间内,然而,我不知道如何绕过逗号-可以得到它的工作没有。
有什么建议吗?
var startDate = new Date('01-01-2021');
var today = new Date();
var diff = Math.floor((today - startDate)/(1000*60*60*24))
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0');
var yyyy = today.getFullYear();
today = mm + '/' + dd + '/' + yyyy;
var answer = diff * 134256;
function commaSeparateNumber(val){
while (/(d+)(d{3})/.test(val.toString())){
val = val.toString().replace(/(d)(?=(ddd)+(?!d))/g, "$1,");
}
return val;
}
document.getElementById("CCalc").innerHTML = (commaSeparateNumber(answer));
<p id="CCalc" class="fig-number"></p>
下面是使用jQuery编写的代码:
var startDate = new Date('01-01-2021');
var today = new Date();
var diff = Math.floor((today - startDate)/(1000*60*60*24))
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0');
var yyyy = today.getFullYear();
today = mm + '/' + dd + '/' + yyyy;
var answer = diff * 134256;
function commaSeparateNumber(val){
while (/(d+)(d{3})/.test(val.toString())){
val = val.toString().replace(/(d)(?=(ddd)+(?!d))/g, "$1,");
}
return val;
}
document.getElementById("CCalc").innerHTML = parseFloat(commaSeparateNumber(answer).replace(/,/g, ''));
$(".fig-number").each(function () {
$(this)
.prop("Counter", 0)
.animate(
{
Counter: $(this).text()
},
{
duration: 3000,
easing: "swing",
step: function (now, tween) {
// Check added for decimal number
if(parseInt(tween.end) == parseFloat(tween.end)){
var number = Math.ceil(now);
$(this).text(number.toLocaleString());
}
},
}
);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p id="CCalc" class="fig-number">39471264</p>