Blogger Information
Blog 6
fans 0
comment 0
visits 4761
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
导航栏下拉菜单实现
遇见
Original
819 people have browsed it

导航栏下拉菜单实现方式之一,结合js、css实现

1.HTML、JavaScript代码,如下:

  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. <!-- 引入外部css样式文件 -->
  7. <link rel="stylesheet" href="css/style.css" />
  8. <title>css定位</title>
  9. </head>
  10. <body>
  11. <div class="navbox">
  12. <ul id="navid">
  13. <li><a href="javascript:void(0)">菜单一</a></li>
  14. <li><a href="javascript:void(0)">菜单二</a></li>
  15. <li><a href="javascript:void(0)">菜单三</a></li>
  16. <li><a href="javascript:void(0)">菜单四</a></li>
  17. <li>
  18. <a href="javascript:void(0)">更多菜单一</a>
  19. <div class="nav-item-box">
  20. <div class="nav-item">子菜单1</div>
  21. <div class="nav-item">子菜单2</div>
  22. <div class="nav-item">子菜单3</div>
  23. </div>
  24. </li>
  25. <li>
  26. <a href="javascript:void(0)">更多菜单二</a>
  27. <div class="nav-item-box">
  28. <div class="nav-item">子菜单4</div>
  29. <div class="nav-item">子菜单5</div>
  30. <div class="nav-item">子菜单6</div>
  31. </div>
  32. </li>
  33. </ul>
  34. </div>
  35. </body>
  36. <script type="text/javascript">
  37. let navbox = document.querySelectorAll(".navbox>ul li");
  38. navbox.forEach(function (li) {
  39. //给一级菜单增加监听事件
  40. li.addEventListener("mouseover", mouseoverFun); //鼠标移动事件
  41. li.addEventListener("mouseout", mouseoutFun); //鼠标移出事件
  42. });
  43. function mouseoverFun(e) {
  44. //判断是否存在子菜单,存在就显示
  45. if (e.target.nextElementSibling != null) {
  46. e.target.nextElementSibling.style.display = "block";
  47. }
  48. }
  49. function mouseoutFun(e) {
  50. console.log(e);
  51. //判断是否存在子菜单,存在就隐藏
  52. if (e.target.nodeName === "A" && e.target.nextElementSibling != null) {
  53. e.target.nextElementSibling.style.display = "none";
  54. }
  55. }
  56. </script>
  57. </html>

2.style.css文类:

  1. * {
  2. padding: 0;
  3. margin: 0;
  4. }
  5. a {
  6. text-decoration: none; /*去掉a标签下划线*/
  7. }
  8. .navbox {
  9. height: 60px;
  10. line-height: 60px; /*块级元素中,垂直居中*/
  11. width: 100%;
  12. background: #000000;
  13. }
  14. .navbox ul li {
  15. position: relative;
  16. list-style: none; /*去掉li前面小黑点*/
  17. float: left;
  18. font-size: 14px;
  19. }
  20. .navbox ul li a {
  21. color: rgba(255, 255, 255, 0.7);/*a标签颜色,最后0.7表示透明度*/
  22. padding: 0 30px;
  23. }
  24. /* 子菜单样式 */
  25. .navbox ul li .nav-item-box {
  26. position: absolute;/*相对最近relative,绝对定位*/
  27. top: 60px;
  28. left: 0;
  29. right: 0;
  30. display: none;
  31. }
  32. .navbox ul li .nav-item-box .nav-item {
  33. height: 35px;
  34. line-height: 35px;
  35. padding: 0 30px;
  36. }

总结:

1.理解了js冒泡(由内向外传递,addEventListener(事件类型,事件方法,false),最后一个参数默认false,可以省略;
2.理解了js捕获穿透(由外向内传递,addEventListener(事件类型,事件方法,true));
3.问题:鼠标移出导航a标签,下面下拉导航就被隐藏,就用上面js方法进行处理,没有思路

Correcting teacher:天蓬老师天蓬老师

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