Blogger Information
Blog 7
fans 0
comment 0
visits 4141
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
第三节 下拉菜单
如今放弃
Original
597 people have browsed it

1:代码

  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. * {
  10. margin: 0;
  11. padding: 0;
  12. box-sizing: border-box;
  13. }
  14. a {
  15. /* color: rgba(255, 255, 255, 0.7); */
  16. color: #bbb;
  17. text-decoration: none;
  18. }
  19. #nav {
  20. background-color: black;
  21. height: 50px;
  22. line-height: 50px;
  23. }
  24. li {
  25. list-style: none;
  26. margin: 0 10px;
  27. float: left;
  28. }
  29. #nav > li > a:hover {
  30. color: white;
  31. }
  32. /* 将父级设置为子菜单的定位容器,即转为定位元素即可 */
  33. #nav > li {
  34. position: relative;
  35. }
  36. #nav > li > ul {
  37. position: absolute;
  38. top: 50px;
  39. width: 180px;
  40. border: 1px solid #aaa;
  41. border-top: none;
  42. }
  43. #nav > li > ul > li a {
  44. display: inline-block;
  45. height: 50px;
  46. color: #444;
  47. }
  48. ul.sub li:hover {
  49. background-color: #eee;
  50. }
  51. /* 初始化时不要显示子菜单 */
  52. #nav > li > ul {
  53. display: none;
  54. }
  55. </style>
  56. </head>
  57. <body>
  58. <ul id="nav">
  59. <li><a href="">显示器</a>
  60. </li>
  61. <li><a href="">主板</a>
  62. <ul>
  63. <li><a href="">华硕</a></li>
  64. <li><a href="">技嘉</a></li>
  65. <li><a href="">七彩虹</a></li>
  66. <li><a href="">昂达</a></li>
  67. </ul>
  68. </li>
  69. <li><a href="">内存</a>
  70. </li>
  71. <li><a href="">硬盘</a>
  72. <ul>
  73. <li><a href="">西数</a></li>
  74. <li><a href="">希捷</a></li>
  75. <li><a href="">日立</a></li>
  76. <li><a href="">三星</a></li>
  77. </ul>
  78. </li>
  79. <li><a href="">显卡</a>
  80. </li>
  81. </ul>
  82. </body>
  83. <script>
  84. const navs =document.querySelectorAll("#nav > li");
  85. navs.forEach(function(nav){
  86. nav.addEventListener("mouseover", showSubMenu);
  87. nav.addEventListener("mouseout", closeSubMenu);
  88. })
  89. function showSubMenu(ev){
  90. if (ev.target.nextElementSibling !==null) {
  91. ev.target.nextElementSibling.style.display = "block";
  92. }
  93. }
  94. function closeSubMenu(ev){
  95. if (ev.target.nextElementSibling !==null) {
  96. ev.target.nextElementSibling.style.display = "none";
  97. }
  98. }
  99. function closeSubMenu(ev) {
  100. if (ev.target.nodeName === "A" && ev.target.nextElementSibling !== null) {
  101. ev.target.nextElementSibling.style.display = "none";
  102. }
  103. }
  104. </script>
  105. </html>

2:效果


Correcting teacher:GuanhuiGuanhui

Correction status:qualified

Teacher's comments:可以!
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post