本文实例为大家分享了js实现数字跳动到指定数字的具体代码,供大家参考,具体内容如下
运行结果如下:
h1 {font-size: 150px;text-align:center;}
p {text-align:center;}
button {font-size:40px;}
0
开始计数
/*
* startNum 代表要跳动的初始数字
* targetNum 代表要跳动到的数字
* time 代表要跳动需要花费的时间
* selector 代表要跳动元素的选择器
*/
const $setJumpNumber = (startNum, targetNum, time = 1, selector) => {
let dom = document.querySelector(selector);
let originNum = startNum;
let stepNum = 0;
let timeNum = time;
dom.innerHTML = startNum;
let timeId = setInterval(() => {
if (originNum < targetNum) {