6.demo.html 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. * {
  9. margin: 0;
  10. padding: 0;
  11. }
  12. .box {
  13. width: 200px;
  14. height: 200px;
  15. background: #ff0;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <div class="box"></div>
  21. <br /><br />
  22. <button id="btn1">放大</button>
  23. <br />
  24. <br />
  25. <button id="btn2">缩小</button>
  26. <script>
  27. var box = document.querySelector(".box");
  28. var btn1 = document.getElementById("btn1");
  29. var btn2 = document.getElementById("btn2");
  30. btn1.onclick = function () {
  31. var timer = setInterval(function () {
  32. box.style.width = box.offsetWidth + 5 + "px";
  33. if (box.offsetWidth >= 400) {
  34. clearInterval(timer);
  35. }
  36. }, 100);
  37. };
  38. btn2.onclick = function () {
  39. var timer = setInterval(function(){
  40. box.style.width = box.offsetWidth - 5 + "px";
  41. if(box.offsetWidth <= 200) {
  42. clearInterval(timer);
  43. }
  44. },100)
  45. }
  46. </script>
  47. </body>
  48. </html>