Blogger Information
Blog 18
fans 3
comment 3
visits 16203
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
导航下拉菜单的简单实现
刹那永恒个人博客
Original
721 people have browsed it

html代码如下

  1. <body>
  2. <ul id="nav">
  3. <li><a href="">首页</a></li>
  4. <li><a href="">视频教程</a></li>
  5. <li>
  6. <a href="">资源下载</a>
  7. <ul>
  8. <li><a href="">PHP工具</a></li>
  9. <li><a href=""></a>在线手册</li>
  10. <li><a href=""></a>学习课件</li>
  11. <li><a href=""></a>网站源码</li>
  12. </ul>
  13. </li>
  14. <li><a href="">社区问答</a></li>
  15. <li>
  16. <a href="">技术文章</a>
  17. <ul>
  18. <li><a href="">头条</a></li>
  19. <li><a href="">博客</a></li>
  20. <li><a href="">PHP教程</a></li>
  21. <li><a href="">PHP框架</a></li>
  22. </ul>
  23. </li>
  24. </ul>
  25. <script>
  26. //获取所有的主导航
  27. const zhudaohang = document.querySelectorAll("#nav > li");
  28. //鼠标进入的情况下检查菜单有没有子菜单,有的话显示子菜单
  29. zhudaohang.forEach(function (xxx) {
  30. //鼠标进入时显示子菜单
  31. xxx.addEventListener("mouseover", xianshi);
  32. //注册鼠标离开的时候的事件
  33. xxx.addEventListener("mouseout", guanbi);
  34. });
  35. //显示子菜单
  36. function xianshi(xx) {
  37. //当前的导航有没有子菜单
  38. if (xx.target.nextElementSibling !== null) {
  39. xx.target.nextElementSibling.style.display = "block";
  40. }
  41. }
  42. //关闭子菜单
  43. function guanbi(xx) {
  44. //当前的导航有没有子菜单,有的话就显示子菜单
  45. if (
  46. xx.target.nodeName === "A" &&
  47. xx.target.nextElementSibling !== null
  48. ) {
  49. xx.target.nextElementSibling.style.display = "none";
  50. }
  51. }
  52. // function guanbi(xx) {
  53. // //当前的导航有没有子菜单,有的话就隐藏子菜单
  54. // if (xx.target.nextElementSibling !== null) {
  55. // xx.target.nextElementSibling.style.display = "none";
  56. // }
  57. // }
  58. </script>
  59. </body>

css代码如下

  1. <style>
  2. /* 元素样式初始化:学到盒模型再详细介绍 */
  3. * {
  4. margin: 0;
  5. padding: 0;
  6. box-sizing: border-box;
  7. }
  8. a {
  9. color: #bbb;
  10. text-decoration: none;
  11. }
  12. #nav {
  13. background-color: black;
  14. height: 50px;
  15. line-height: 50px;
  16. }
  17. li {
  18. list-style: none;
  19. margin: 0 10px;
  20. float: left;
  21. }
  22. #nav > li > a:hover {
  23. color: white;
  24. }
  25. /* 将父级设置为子菜单的定位容器,即转为定位元素即可 */
  26. #nav > li {
  27. position: relative;
  28. }
  29. #nav > li > ul {
  30. position: absolute;
  31. top: 50px;
  32. width: 180px;
  33. border: 1px solid #aaa;
  34. border-top: none;
  35. }
  36. #nav > li > ul > li a {
  37. display: inline-block;
  38. height: 50px;
  39. color: #444;
  40. }
  41. ul.sub li:hover {
  42. background-color: #eee;
  43. }
  44. /* 初始化时不需要显示子菜单 */
  45. #nav > li > ul {
  46. display: none;
  47. }
  48. </style>

实际效果


总结 刚学完这节课晕晕的,回放看了几遍,陌生命令仔细研究了一遍,感觉熟悉了很多

Correcting teacher:WJWJ

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