// 点击到 400
my$("btn1").onclick = function() {
animate(my$("dv"), 400);
};
// 点击到 800
my$("btn2").onclick = function() {
animate(my$("dv"), 800);
};
function animate(ele, target) {
clearInterval(ele.timeId);
ele.timeId = setInterval(function() {
// 获取当前位置
var current = ele.offsetLeft;
// 每次移动多少
var step = (target - current) / 10;
step = step > 0 ? Math.ceil(step) : Math.floor(step);
// 每次移动后的位置
current += step;
// 设置目标位置
// 判断
if (current == target) {
clearInterval(ele.timeId);
}
ele.style.left = current + "px";
}, 20);
}
缓动动画函数
最新推荐文章于 2022-10-23 23:07:58 发布