方案1
<h1 id="numBox"></h1>
<script>
function numRunFun(num, maxNum){
var numBox = document.getElementById("numBox");
var num = num;
var maxNum = maxNum;
var timer = setInterval(function(){
num++;
if(num >= maxNum){
numBox.innerHTML = maxNum;
clearInterval(timer);
} else {
numBox.innerHTML = ~~(num);
}
},100);
}
numRunFun(0, 10);
</script>
方案2
<h1 id="numBox"></h1>
<script>
function numRunFun(num, maxNum) {
var num = num;
var maxNum = maxNum;
var numId = document.getElementById("numBox");
function numSlideFun(){
num++;
if(num >= maxNum){
numId.innerHTML = maxNum;
return;
}else{
numId.innerHTML = ~~(num);
setTimeout(function(){
return numSlideFun();
},100);
}
}
numSlideFun();
}
</script>
方案3
<h1 id="numBox"></h1>
<script>
var numId = document.getElementById("numBox");
function NumAutoPlusAnimation(startNum, endNum ) {
var step = (endNum - startNum) / 20;
var count = startNum;
var timer = setInterval(() => {
count = count + step;
if (count >= endNum) {
clearInterval(timer);
count = endNum;
}
numId.innerHTML = count.toFixed(2)
}, 30);
}
NumAutoPlusAnimation(0, 26.88);
</script>
|