6_动画.html 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 1s 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 div1 = document.getElementById('div1')
  27. var up = document.getElementById('up')
  28. var down = document.getElementById('down')
  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 = '500px'
  38. // }
  39. up.onclick = function(){
  40. div1.style.width = '500px'
  41. }
  42. down.onclick = function(){
  43. var timer2 = setInterval(function(){
  44. if(div1.offsetWidth > 300){
  45. div1.style.width = div1.offsetWidth - 10 + 'px'
  46. } else {
  47. clearInterval(timer2)
  48. }
  49. },10)
  50. }
  51. </script>
  52. </body>
  53. </html>