练习9_三道杠.html 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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: 50px;
  11. background-color: red;
  12. margin-top: 20px;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <div class="box"></div>
  18. <div class="box"></div>
  19. <div class="box"></div>
  20. <script>
  21. var aBox = document.getElementsByClassName("box");
  22. var timer1 = null;
  23. for (var i = 0; i < aBox.length; i++) {
  24. aBox[i].index = i;
  25. aBox[i].onmouseover = function () {
  26. var _index = this.index;
  27. clearInterval(aBox[_index].timer2)
  28. aBox[_index].timer1 = setInterval(function () {
  29. var _width = aBox[_index].offsetWidth;
  30. if (_width >= 600) {
  31. clearInterval(aBox[_index].timer1)
  32. } else {
  33. _width += 10;
  34. aBox[_index].style.width = _width + "px";
  35. }
  36. }, 16)
  37. }
  38. aBox[i].onmouseout = function () {
  39. var _index = this.index;
  40. clearInterval(aBox[_index].timer1)
  41. aBox[_index].timer2 = setInterval(function () {
  42. var _width = aBox[_index].offsetWidth;
  43. if (_width <= 200) {
  44. clearInterval(aBox[_index].timer2)
  45. } else {
  46. _width -= 10;
  47. aBox[_index].style.width = _width + "px";
  48. }
  49. }, 16)
  50. }
  51. }
  52. </script>
  53. </body>
  54. </html>