js动画缓动效果实现setInterval(),就是慢慢的停下来

<!DOCTYPE HTML>
<html>
 <head>
  <title>js动画缓动效果实现setInterval(),就是慢慢的停下来</title>
  <meta charset="utf-8" />
  <style>
   * {
    margin: 0;
    padding: 0;
    }
   #box {
    
    width: 100px;
    height: 100px;
    background-color: pink;
   }
  </style>
 </head>
 <body>
  <div id="box"> </div>
  <script>
   //第一步 获得盒子的当前位置
   var box = document.getElementById('box');
   //每次移动的距离 = (目标值-现在的位置)/10
   function move() {
    box.style.marginLeft = box.offsetLeft + (300 - box.offsetLeft) / 10 + 'px';
    if(box.offsetLeft == 300) {
      clearTimeout(timer);
     }
   }
   //用定时器让盒子动起来 
   var timer = setInterval(move, 100);
  </script>
 </body>
</html>
原创文章 22 获赞 4 访问量 2594

猜你喜欢

转载自blog.csdn.net/tuzi007a/article/details/105463824