6_动画.html 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <style>
  9. * {
  10. margin: 0;
  11. padding: 0;
  12. }
  13. #div1 {
  14. width: 200px;
  15. height: 200px;
  16. background: red;
  17. transition: width 2s linear;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <button id="up">放大</button>
  23. <div id="div1"></div>
  24. <button id="down">缩小</button>
  25. <script>
  26. var up = document.getElementById('up')
  27. var down = document.getElementById('down')
  28. var div1 = document.getElementById('div1')
  29. // up.onclick = function () {
  30. // var timer = setInterval(function () {
  31. // if (div1.offsetWidth < 500) {
  32. // div1.style.width = div1.offsetWidth + 10 + 'px'
  33. // } else {
  34. // clearInterval(timer)
  35. // }
  36. // }, 10);
  37. // // div1.style.width = 500 + 'px'
  38. // }
  39. up.onclick = function () {
  40. div1.style.width = 500 + 'px'
  41. }
  42. down.onclick = function () {
  43. var timer1 = setInterval(function () {
  44. if (div1.offsetWidth > 0) {
  45. div1.style.width = div1.offsetWidth - 10 + 'px'
  46. } else {
  47. clearInterval(timer1)
  48. }
  49. }, 10)
  50. }
  51. </script>
  52. </body>
  53. </html>