首页 > 其他 > 详细

动画setInterval

时间:2020-04-07 20:09:06      阅读:55      评论:0      收藏:0      [点我收藏+]

var timerId = null;
// 封装动画的函数
function animate(element, target) {

// 通过判断,保证页面上只有一个定时器在执行动画
if (timerId) {
clearInterval(timerId);
timerId = null;
}

timerId = setInterval(function () {
// 步进 每次移动的距离
var step = 10;
// 盒子当前的位置
var current = element.offsetLeft;

if (current >= target) {
// 让定时器停止
clearInterval(timerId);
// 让盒子到target的位置
element.style.left = target + ‘px‘;
return;
}
// 移动盒子
current += step;
element.style.left = current + ‘px‘;
}, 30);
}

动画setInterval

原文:https://www.cnblogs.com/pxxdbk/p/12655039.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!