Js-task.html 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .button {
  6. display: inline-block;
  7. padding: 10px 20px;
  8. background-color: #3498db;
  9. color: #fff;
  10. border: none;
  11. cursor: pointer;
  12. transition: background-color 0.3s ease;
  13. }
  14. .button:hover {
  15. background-color: #2980b9;
  16. }
  17. .dropdown {
  18. position: relative;
  19. display: inline-block;
  20. margin-top: 10px;
  21. }
  22. .dropdown-content {
  23. display: none;
  24. position: absolute;
  25. background-color: #f9f9f9;
  26. min-width: 200px;
  27. box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  28. z-index: 1;
  29. }
  30. .dropdown:hover .dropdown-content {
  31. display: block;
  32. }
  33. .dropdown-item {
  34. padding: 10px;
  35. cursor: pointer;
  36. transition: background-color 0.3s ease;
  37. }
  38. .dropdown-item:hover {
  39. background-color: #ddd;
  40. }
  41. </style>
  42. </head>
  43. <body>
  44. <div class="dropdown">
  45. <button class="button">按钮1</button>
  46. <div class="dropdown-content">
  47. <div class="dropdown-item">选项1</div>
  48. <div class="dropdown-item">选项2</div>
  49. <div class="dropdown-item">选项3</div>
  50. </div>
  51. </div>
  52. <div class="dropdown">
  53. <button class="button">按钮2</button>
  54. <div class="dropdown-content">
  55. <div class="dropdown-item">选项4</div>
  56. <div class="dropdown-item">选项5</div>
  57. <div class="dropdown-item">选项6</div>
  58. </div>
  59. </div>
  60. <div class="dropdown">
  61. <button class="button">按钮3</button>
  62. <div class="dropdown-content">
  63. <div class="dropdown-item">选项7</div>
  64. <div class="dropdown-item">选项8</div>
  65. <div class="dropdown-item">选项9</div>
  66. </div>
  67. </div>
  68. <div class="dropdown">
  69. <button class="button">按钮4</button>
  70. <div class="dropdown-content">
  71. <div class="dropdown-item">选项10</div>
  72. <div class="dropdown-item">选项11</div>
  73. <div class="dropdown-item">选项12</div>
  74. </div>
  75. </div>
  76. <script>
  77. var dropdowns = document.getElementsByClassName("dropdown");
  78. for (var i = 0; i < dropdowns.length; i++) {
  79. dropdowns[i].addEventListener("mouseover", function () {
  80. this.children[1].style.display = "block";
  81. });
  82. dropdowns[i].addEventListener("mouseout", function () {
  83. this.children[1].style.display = "none";
  84. });
  85. }
  86. </script>
  87. </body>
  88. </html>