Blogger Information
Blog 31
fans 2
comment 0
visits 27675
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
导航下拉菜单
霏梦
Original
772 people have browsed it
  • 作者:霏梦

实现步骤

  • 先定义一个导航
  • 通过无序列表,定义好要显示的菜单
  • 通过布局实现样式
  • 通过事件来实现下拉效果

    代码如下

  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>下拉菜单</title>
  7. <style>
  8. /* 元素样式初始化 */
  9. * {
  10. margin: 0;
  11. padding: 0;
  12. box-sizing: border-box;
  13. }
  14. a {
  15. color: #bbb;
  16. text-decoration: none;
  17. }
  18. #nav {
  19. background-color: black;
  20. height: 50px;
  21. line-height: 50px;
  22. }
  23. li {
  24. list-style: none;
  25. margin: 0 10px;
  26. float: left;
  27. }
  28. #nav > li > a:hover {
  29. color: white;
  30. }
  31. #nav > li {
  32. position: relative;
  33. }
  34. #nav > li > ul {
  35. position: absolute;
  36. top: 50px;
  37. width: 180px;
  38. border: 1px solid #aaa;
  39. border-top: none;
  40. }
  41. #nav > li > ul > li a {
  42. display: inline-block;
  43. height: 50px;
  44. color: #444;
  45. }
  46. ul.sub li:hover {
  47. background-color: #eee;
  48. }
  49. /* 初始化时不要显示 */
  50. #nav > li > ul {
  51. display: none;
  52. }
  53. </style>
  54. </head>
  55. <body>
  56. <ul id="nav">
  57. <li><a href="">首页</a></li>
  58. <li>
  59. <a href="">公司产品</a>
  60. <ul>
  61. <li><a href="">在线聊天</a></li>
  62. <li><a href="">在线看电影</a></li>
  63. <li><a href="">在线学习</a></li>
  64. <li><a href="">网站源码</a></li>
  65. <li><a href=""></a></li>
  66. </ul>
  67. </li>
  68. <li><a href="">寻找帮助</a></li>
  69. <li>
  70. <a href="">公司地点</a>
  71. <ul>
  72. <li><a href="">总部</a></li>
  73. <li><a href="">分部</a></li>
  74. <li><a href="">事业部</a></li>
  75. <li><a href="">海外</a></li>
  76. </ul>
  77. </li>
  78. </ul>
  79. </body>
  80. <script>
  81. // 获取所有的主导航
  82. const navs = document.querySelectorAll("#nav>li");
  83. //console.log(navs);
  84. navs.forEach(function (nav) {
  85. //鼠标移入时,显示子菜单
  86. nav.addEventListener("mouseover", showSubMenu);
  87. //鼠标移出时,关掉子菜单
  88. nav.addEventListener("mouseout", closeSubMenu);
  89. });
  90. function showSubMenu(ev) {
  91. //判断当前这个导航有没有子菜单
  92. if (ev.target.nextElementSibling !== null) {
  93. ev.target.nextElementSibling.style.display = "block";
  94. }
  95. }
  96. //判断有没有元素标签A
  97. function closeSubMenu(ev) {
  98. if (ev.target.nodeName === "A" && ev.target.nextElementSibling !== null) {
  99. ev.target.nextElementSibling.style.display = "none";
  100. }
  101. }
  102. </script>
  103. </html>
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:下拉菜单 , 有多种实现的dom结构, 目前 咱们教的是最经典的, 当然并不是最简洁的, 以后学到flex会发现还可以大量简化代码的
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