function slowAnimate(element, target) {
clearInterval(element.slowId);
element.slowId = setInterval(function () {
//获取元素当前位置
var current = element.offsetLeft;
//移动步数
var step = (target - current) / 10;
//step为正数遇到小数时就向上取整,step为负数遇到小数时就向下取整
step = step > 0 ? Math.ceil(step) : Math.floor(step);
current += step;
//如果当前位置不等于目标位置
element.style.left = current + "px";
if (current == target) {
clearInterval(element.slowId);
}
//测试
console.log("目标位置:" + target + ",当前位置:" + current + ",每次移动步数:" + step);
}, 20);
}
封装减速动画
最新推荐文章于 2022-04-20 19:45:26 发布