8.动画.html 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <style>
  8. #box {
  9. width: 200px;
  10. height: 200px;
  11. background: #00f;
  12. /* 平滑的过渡效果
  13. transition:
  14. 执行效果的元素属性 执行完的时间 执行时的运动状态 延迟时间
  15. linear 规定以相同速度开始至结束的过渡效果(等于 cubic-bezier(0,0,1,1))。
  16. ease 规定慢速开始,然后变快,然后慢速结束的过渡效果(cubic-bezier(0.25,0.1,0.25,1))。
  17. ease-in 规定以慢速开始的过渡效果(等于 cubic-bezier(0.42,0,1,1))。
  18. ease-out 规定以慢速结束的过渡效果(等于 cubic-bezier(0,0,0.58,1))。
  19. ease-in-out 规定以慢速开始和结束的过渡效果(等于 cubic-bezier(0.42,0,0.58,1))。
  20. cubic-bezier(n,n,n,n) 在 cubic-bezier 函数中定义自己的值。可能的值是 0 至 1 之间的数
  21. */
  22. transition: width 2s linear 0;
  23. }
  24. </style>
  25. </head>
  26. <body>
  27. <div id="box"></div>
  28. <button id="btn1">放大</button>
  29. <button id="btn2">缩小</button>
  30. <script>
  31. var box = document.getElementById("box");
  32. var btn1 = document.getElementById("btn1");
  33. var btn2 = document.getElementById("btn2");
  34. btn1.onclick = function() {
  35. console.log("走进来")
  36. var timer = setInterval(function(){
  37. box.style.width = box.offsetWidth + 10 + 'px';
  38. if(box.offsetWidth >= 500) {
  39. clearInterval(timer);
  40. }
  41. },20)
  42. }
  43. </script>
  44. </body>
  45. </html>