Blogger Information
Blog 8
fans 0
comment 0
visits 8394
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
下拉框练习
努力努力再努力
Original
871 people have browsed it

模仿php中文网的导航

学习体会:

1.定位,如果li标签不设置定位属性,则下拉框会使相邻的li标签位置发生偏移

2.事件监听:addEventListener()绑定鼠标移入移出事件

3.如果被绑定者也就是ev.currentTarget是li标签的话 在鼠标移出的时候会发生事件冒泡,导致被绑定的li标签也会隐藏,此时需要设置一个判断条件ev.target.nodeName===’A’即可,但是我是直接将被绑定者设置为a标签,这样的话li标签并没绑定事件,事件冒泡则不会影响li标签。

  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. margin: 0px;
  10. padding: 0px;
  11. box-sizing: border-box;
  12. }
  13. .nav {
  14. background-color: black;
  15. width: 100%;
  16. height: 50px;
  17. line-height: 50px;
  18. }
  19. /* 去掉列表样式 */
  20. li {
  21. list-style: none;
  22. float: left;
  23. margin-left: 20px;
  24. }
  25. /* 去掉下划线 */
  26. a {
  27. text-decoration: none;
  28. }
  29. /* 设置浮动和定位 */
  30. .nav > ul > li {
  31. float: left;
  32. margin-left: 15px;
  33. position: relative;
  34. }
  35. /* 设置悬停 */
  36. .nav > ul > li > a:hover {
  37. color: honeydew;
  38. }
  39. .nav > ul > li > ul {
  40. position: absolute;
  41. top: 50px;
  42. font-size: 0.5rem;
  43. border: 1px solid black;
  44. width: 180px;
  45. border-top: none;
  46. }
  47. .nav li > ul > li > a {
  48. display: inline-block;
  49. height: 50px;
  50. width: 65px;
  51. }
  52. .nav > ul > li > ul > li > a:hover {
  53. background-color: antiquewhite;
  54. }
  55. /* 隐藏下拉框 */
  56. .nav > ul > li > ul {
  57. display: none;
  58. }
  59. </style>
  60. </head>
  61. <body>
  62. <div class="nav">
  63. <ul>
  64. <li><a href="">首页</a></li>
  65. <li><a href="">视频教程</a></li>
  66. <li><a href="">入门教程</a></li>
  67. <li><a href="">社区问答</a></li>
  68. <li>
  69. <a href="">技术文章</a>
  70. <ul>
  71. <li><a href="">头条</a></li>
  72. <li><a href="">博客</a></li>
  73. <li><a href="">php教程</a></li>
  74. <li><a href="">php框架</a></li>
  75. <li><a href="">js教程</a></li>
  76. <li><a href="">mysql教程</a></li>
  77. <li><a href="">php小知识</a></li>
  78. <li><a href="">css教程</a></li>
  79. <li><a href="">html教程</a></li>
  80. <li><a href="">服务器运维</a></li>
  81. </ul>
  82. </li>
  83. <li>
  84. <a href="">资源下载</a>
  85. <ul>
  86. <li><a href="">php工具</a></li>
  87. <li><a href="">在线工具</a></li>
  88. <li><a href="">手册下载</a></li>
  89. <li><a href="">学习课件</a></li>
  90. <li><a href="">js特效</a></li>
  91. <li><a href="">后端模板</a></li>
  92. <li><a href="">网站源码</a></li>
  93. <li><a href="">类库下载</a></li>
  94. </ul>
  95. </li>
  96. <li><a href="">工具下载</a></li>
  97. <li><a href="">php培训</a></li>
  98. </ul>
  99. <script>
  100. // 获取被点击a标签
  101. const as = document.querySelectorAll(".nav>ul>li>a");
  102. // 循环 判断是否有兄弟节点 创建监听事件
  103. as.forEach(function (a) {
  104. //鼠标移入
  105. a.addEventListener("mouseover", showMenu);
  106. //鼠标移出
  107. a.addEventListener("mouseout", closeMenu);
  108. });
  109. function showMenu(ev) {
  110. // console.log(ev.target.nextElementSibling);
  111. // 判断是否有兄弟节点 有的话则打开下拉菜单
  112. if (ev.target.nextElementSibling !== null) {
  113. //console.log(ev.target.nextElementSibling);
  114. ev.target.nextElementSibling.style.display = "block";
  115. }
  116. }
  117. function closeMenu(ev) {
  118. if (ev.target.nextElementSibling !== null) {
  119. // console.log(ev.target.nextElementSibling);
  120. ev.target.nextElementSibling.style.display = "none";
  121. }
  122. }
  123. </script>
  124. </div>
  125. </body>
  126. </html>

结果图


Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:当时打算将html,css,js同步教学的, 同学们反映太难, 所以这个作业完成有难度
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