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>






转载于:https://www.cnblogs.com/webfby/p/5995878.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值