封装动画

 // obj  目标对象  target 目标位置  
 function animate(obj, target, callback) {
     // 清除原先定时器  
     clearInterval(obj.timer);
     obj.timer = setInterval(function() {
         // 步长值写到定时器的里面
         // 把步长改为整数 
         // var step = Math.ceil((target - obj.offsetLeft) / 10);
         var step = step = (target - obj.offsetLeft) / 10;
         step = step > 0 ? Math.ceil(step) : Math.floor(step);

         if (obj.offsetLeft == target) {
             // 停止动画
             clearInterval(obj.timer);
             // 回调函数写到定时器结束里面
             if (callback) {
                 // 调用函数
                 callback();
             }
         }
         // 把步长 改为慢慢变小的值  步长公式 (目标值 - 现在的位置) /10
         obj.style.left = obj.offsetLeft + step + 'px';
     }, 15)
 }
原文地址:https://www.cnblogs.com/ericblog1992/p/13061553.html