【发布时间】:2020-11-09 05:35:24
【问题描述】:
我使用Math.random() 使用随机数创建了计数器编号功能。我已经制作了如下代码,但仍有一些事情不能很好地工作。我有几个号码,例如2001.9 和1500。问题是,当动画运行其他数字时,但在数字1.9 中,动画不起作用。还有,速度能不能再慢一点?
我创建的代码中的缺失或错误在哪里?
// function for count statistic
function countNumber() {
$('.count').each(function() {
var countTo = Number($(this).text())
$(this).prop('Counter', 0).animate({
Counter: countTo - 1
}, {
duration: 2000,
easing: 'swing',
step: function(now) {
var ceil = Math.floor(Math.random() * Math.floor(now))
if (ceil < countTo) {
$(this).text(ceil);
}
},
complete: function() {
$(this).text(countTo);
}
});
});
}
$(document).ready(function() {
countNumber();
});
#countAnimation {
display: flex;
justify-content: space-between;
margin-bottom:200px;
}
.box-counter {
display: flex;
justify-content: center;
align-items: center;
background-color: #cacaca;
color: #0f0f0f;
width: 150px;
height: 150px;
border-radius: 50%;
box-shadow: 0px 6px 12px rgba(0, 0, 0, 0.16);
font-size: 50px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="countAnimation">
<div class="box-counter">
<div class="count">200</div>
</div>
<div class="box-counter">
<div class="count">1.9</div>
</div>
<div class="box-counter">
<div class="count">1500</div>
</div>
</div>
【问题讨论】:
-
因为 2000 大于 1.9 所以 if (ceil
-
那么,怎么做?你能给我一个我制作的代码的例子吗? @nAviD
-
拳头你告诉你要怎么数到1.9?
标签: javascript jquery animation jquery-animate counter