【发布时间】:2020-03-05 14:32:00
【问题描述】:
知道如何自动将值设置为未来日期和今天日期的差值吗?我正在努力让这段代码工作。
<span class="count">200</span>
<span class="count2">200.7</span>
<span class="count" id="reducedaily">1000</span>
const oneDay = 24 * 60 * 60 * 1000; // hours*minutes*seconds*milliseconds
const firstDate = new Date(2020, 10, 2);
const secondDate = new Date();
const diffDays = Math.round(Math.abs((firstDate - secondDate) / oneDay));
jQuery(#reducedaily).text(diffDays);
jQuery('.count, .count2').each(function () {
jQuery(this).prop('Counter',0).animate({
Counter: jQuery(this).text()
}, {
duration: 4000,
easing: 'swing',
step: function (now) {
if(jQuery(this).hasClass('count')){
jQuery(this).text(now.toFixed(0));
}
else{
jQuery(this).text(now.toFixed(1));
}
}
});
});
【问题讨论】:
-
如何确定开始日期,然后根据当前日期计算计数?像(1000 -(2020/01/01)-今天)
-
我认为
jQuery(#reducedaily).text(diffDays)的错误只是您的代码示例中的错字
标签: javascript jquery counter reduce