function animation(obj, distance) {
clearInterval(obj.timer); //清掉计时器
obj.timer = setInterval(() => { //注册计时器
let opotioning = obj.offsetLeft; //获取当前的位置
let step = 10; //每次移动的步数
opotioning < distance ? step : step = -step; //判断往前还是往后 往后是负值
//判断是否已经达到了目的地,到了关闭及时器
if (Math.abs(opotioning - distance) >= Math.abs(step)) { //判断是否有 足够远的路程让你来 完成这一步
obj.style.left = opotioning + step + 'px';
} else {
obj.style.left = distance + 'px';
clearInterval(obj.timer)
}
}, 15)
}
js函数封装动画
猜你喜欢
转载自blog.csdn.net/weixin_55333190/article/details/117913932
今日推荐
周排行