JavaScript基础学习 引用move.js的动画
Document
move.js
// 给不同元素指定不同的定时器 function move(obj,target,callback){ // 想要调用定时器时首先就需要清除已有的定时器,然后再重新启动 clearInterval(obj.timer); obj.timer=setInterval(function(){ var step =(target-obj.offsetLeft)/10; // step =Math.ceil(step); step = step>0 ? Math.ceil(step):Math.floor(step); if(obj.offsetLeft == target){ clearInterval(obj.timer); console.log(22222222222); // 回调函数写在定时器之后,在定时器停止之后执行该函数 if(callback){ callback(); } }else{ obj.style.left=obj.offsetLeft + step + 'px'; } },30) }