酷炫数据营销平台

ceshi.html 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <title>Document</title>
  5. <style>
  6. #e{
  7. width: 100px;
  8. height: 100px;
  9. background: red;
  10. position: absolute;
  11. left: 0;
  12. top: 0;
  13. zoom: 1;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <div id="e"></div>
  19. <script>
  20. var e = document.getElementById("e");
  21. var flag = true;
  22. var left = 0;
  23. //当前执行时间
  24. var nowTime = 0;
  25. //记录每次动画执行结束的时间
  26. var lastTime = Date.now();
  27. //我们自己定义的动画时间差值
  28. var diffTime = 40;
  29. function render() {
  30. if(flag == true){
  31. if(left>=100){
  32. flag = false
  33. }
  34. e.style.left = ` ${left++}px`
  35. }else{
  36. if(left<=0){
  37. flag = true
  38. }
  39. e.style.left = ` ${left--}px`
  40. }
  41. }
  42. //requestAnimationFrame效果
  43. (function animloop() {
  44. //记录当前时间
  45. nowTime = Date.now()
  46. // 当前时间-上次执行时间如果大于diffTime,那么执行动画,并更新上次执行时间
  47. if(nowTime-lastTime > diffTime){
  48. lastTime = nowTime
  49. render();
  50. }
  51. requestAnimationFrame(animloop);
  52. })()
  53. </script>
  54. </body>
  55. </html>