代码:
increasingNumberAnimation = (selector, start, end, step = 1, duration = 2000) => {
let current = start,
_step = (end - start) * step < 0 ? -step : step,
timer = setInterval(() => {
current += _step;
document.querySelector(selector).innerHTML = current;
if (current >= end) document.querySelector(selector).innerHTML = end;
if (current >= end) clearInterval(timer);
}, Math.abs(Math.floor(duration / ((end - start) / _step))));
return timer;
};
使用说明:
seletor:dom容器 如:#my-id .my-class
start:初始值
end:结束值
step:步长,默认1
duration:执行时长,单位ms
注意事项:
setInterval最小为10ms,如果起始值与结束值相差太大,且步长过短,时间过短可能会使功能无效