16-js-缓冲运动

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <style>
  7. * {
  8. margin: 0;
  9. padding: 0;
  10. }
  11. #div1 {
  12. width: 100px;
  13. height: 100px;
  14. background-color: red;
  15. position: absolute;
  16. left: 600px;
  17. top: 50px;
  18. }
  19. #div2 {
  20. width: 1px;
  21. height: 300px;
  22. position: absolute;
  23. left: 300px;
  24. top: 0;
  25. background-color: black;
  26. }
  27. </style>
  28. <script>
  29. function startMove() {
  30. var oDiv = document.getElementById(\'div1\');
  31. setInterval(function () {
  32. var speed = (300 - oDiv.offsetLeft) / 10;
  33. //speed = Math.ceil(speed);//向上取整
  34. speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);//判断是否大于0, 向上or向下取整;
  35. oDiv.style.left = oDiv.offsetLeft + speed + \'px\';
  36. document.title = oDiv.offsetLeft + \',\' + speed;
  37. }, 30);
  38. }
  39. </script>
  40. </head>
  41. <body>
  42. <input type="button" value="开始运动" onclick="startMove()">
  43. <div id="div1"></div>
  44. <div id="div2"></div>
  45. </body>
  46. </html>






posted @ 2016-10-25 10:58  webfby  阅读(116)  评论(0编辑  收藏  举报