显示计数器的十进制值
Show decimal value of counter
如何只显示一个计数器的十进制值?目前将 4.7 舍入为 5
$('.count').each(function() {
$(this).prop('Counter', 0).animate({
Counter: jQuery(this).text()
}, {
duration: 4000,
easing: 'swing',
step: function(now) {
$(this).text(Math.ceil(now));
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="shiva"><span class="count">200</span></div>
<div id="shiva"><span class="count" id="reducedaily">4.7</span></div>
您可以尝试使用 toFixed()
:
The toFixed()
method formats a number using fixed-point notation.
jQuery('.count').each(function () {
jQuery(this).prop('Counter',0).animate({
Counter: jQuery(this).text()
}, {
duration: 4000,
easing: 'swing',
step: function (now) {
if(this.id == 'reducedaily'){
jQuery(this).text(now.toFixed(1));
}
else{
jQuery(this).text(now.toFixed(0));
}
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="shiva"><span class="count">200</span></div>
<div id="shiva2"><span class="count" id="reducedaily">4.7</span></div>
请注意:属性id在文档中必须是唯一的。
如何只显示一个计数器的十进制值?目前将 4.7 舍入为 5
$('.count').each(function() {
$(this).prop('Counter', 0).animate({
Counter: jQuery(this).text()
}, {
duration: 4000,
easing: 'swing',
step: function(now) {
$(this).text(Math.ceil(now));
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="shiva"><span class="count">200</span></div>
<div id="shiva"><span class="count" id="reducedaily">4.7</span></div>
您可以尝试使用 toFixed()
:
The
toFixed()
method formats a number using fixed-point notation.
jQuery('.count').each(function () {
jQuery(this).prop('Counter',0).animate({
Counter: jQuery(this).text()
}, {
duration: 4000,
easing: 'swing',
step: function (now) {
if(this.id == 'reducedaily'){
jQuery(this).text(now.toFixed(1));
}
else{
jQuery(this).text(now.toFixed(0));
}
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="shiva"><span class="count">200</span></div>
<div id="shiva2"><span class="count" id="reducedaily">4.7</span></div>
请注意:属性id在文档中必须是唯一的。