function NumAutoPlusAnimation(targetEle, options) {
/可以自己改造下传入的参数,按照自己的需求和喜好封装该函数/
//不传配置就把它绑定在相应html元素的data-xxxx属性上吧
options = options || {};
// var this = targetEle;
let oldVal = options.oldVal;
let time = options.time, //总时间--毫秒为单位
finalNum = options.num, //要显示的真实数值
regulator = options.regulator || 100, //调速器,改变regulator的数值可以调节数字改变的速度
step = time / (finalNum - oldVal) /每30ms增加的数值--/,
initial = 0;
// console.log(finalNum - oldVal);
let shouldAdd = oldVal < finalNum ? true : false;
timers = setInterval(function() {
oldVal = oldVal + step / Math.abs(step);
// console.log(shouldAdd, step);
if (shouldAdd) {
if (oldVal >= finalNum) {
clearInterval(timers);
timers = null;
oldVal = finalNum;
}
} else {
if (oldVal <= finalNum) {
clearInterval(timers);
timers = null;
oldVal = finalNum;
}
}
//t未发生改变的话就直接返回
//避免调用text函数,提高DOM性能
var t = Math.floor(oldVal);
if (t == initial) return;
// clearInterval(timer);
initial = t;
$this.innerHTML = initial + "%";
}, regulator);
}