Blogger Information
Blog 8
fans 1
comment 0
visits 8882
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
下拉菜单的css样式和js代码解析
BigPig
Original
911 people have browsed it

二级菜单栏的样式与 js 脚本编写

  1. <html>
  2. <head>
  3. <style>
  4. .dh {
  5. margin: 0 auto;
  6. height: 50px;
  7. width: 1400px;
  8. }
  9. .dh li {
  10. list-style-type: none; /* 去掉li前的点 */
  11. float: left; /*将li设置成做浮动,变为联动*/
  12. }
  13. .dh a {
  14. display: block; /*将a变成块状*/
  15. width: 100px; /*设置块的宽度*/
  16. height: 50px; /*设置块的长度*/
  17. font-family: Microsoft Yahei;
  18. line-height: 50px; /*设置字体在块中的高度*/
  19. background-color: #2f4f4f;
  20. margin: 0px 0px; /*块里的高宽通过margin设置*/
  21. color: #fff;
  22. text-align: center; /*字体居中*/
  23. text-decoration: none; /*去掉下划线*/
  24. font-size: 15px;
  25. }
  26. .dh a:hover {
  27. background-color: #2f6f4f;
  28. }
  29. li > dl {
  30. margin-top: 10px;
  31. position: absolute;
  32. width: 200px;
  33. height: auto;
  34. background-color: lime;
  35. display:none;
  36. }
  37. dl > dd {
  38. margin-left: 0px;
  39. width: 100px;
  40. height: 50px;
  41. float: left;
  42. background-color: mediumslateblue;
  43. }
  44. </style>
  45. </head>
  46. <body>
  47. <div class="dh">
  48. <ul id="nav">
  49. <li>
  50. <a href="">首页</a>
  51. <dl>
  52. <dd><a href="">二级菜单</a></dd>
  53. <dd><a href="">二级菜单</a></dd>
  54. <dd><a href="">二级菜单</a></dd>
  55. <dd><a href="">二级菜单</a></dd>
  56. <dd><a href="">二级菜单</a></dd>
  57. <dd><a href="">二级菜单</a></dd>
  58. </dl>
  59. </li>
  60. <li>
  61. <a href="">摄像技巧</a>
  62. </li>
  63. <li>
  64. <a href="">标准摄像配置</a>
  65. </li>
  66. <li>
  67. <a href="">摄像教程</a>
  68. </li>
  69. <li>
  70. <a href="">摄像作品</a>
  71. </li>
  72. <li>
  73. <a href="">摄像作品上传</a>
  74. <dl>
  75. <dd><a href="">二级菜单</a></dd>
  76. <dd><a href="">二级菜单</a></dd>
  77. <dd><a href="">二级菜单</a></dd>
  78. <dd><a href="">二级菜单</a></dd>
  79. <dd><a href="">二级菜单</a></dd>
  80. <dd><a href="">二级菜单</a></dd>
  81. </dl>
  82. </li>
  83. <li>
  84. <a href="">汽车</a>
  85. </li>
  86. <li>
  87. <a href="">影赛</a>
  88. </li>
  89. <li>
  90. <a href="">论坛</a>
  91. <dl>
  92. <dd><a href="">二级菜单</a></dd>
  93. <dd><a href="">二级菜单</a></dd>
  94. <dd><a href="">二级菜单</a></dd>
  95. <dd><a href="">二级菜单</a></dd>
  96. <dd><a href="">二级菜单</a></dd>
  97. <dd><a href="">二级菜单</a></dd>
  98. </dl>
  99. </li>
  100. </ul>
  101. </div>
  102. <script type="text/javascript">
  103. //获取有navid的元素下的所有li
  104. var navs = document.querySelectorAll("#nav > li");
  105. //用forEach循环遍历所有的li标签,并且为其添加事件监听器
  106. navs.forEach(function (nav) {
  107. nav.addEventListener("mouseover", showSubMenu);
  108. nav.addEventListener("mouseout", closeSubMenu);
  109. })
  110. function showSubMenu(ev) {
  111. if (ev.target.nextElementSibling !== null) {
  112. ev.target.nextElementSibling.style.display = "block";
  113. }
  114. }
  115. function closeSubMenu(ev) {
  116. if (ev.target.nodeName === "A" && ev.target.nextElementSibling !== null) {
  117. ev.target.nextElementSibling.style.display = "none";
  118. }
  119. }
  120. </script>
  121. </bdoy>
  122. </html>

以上就是下拉菜单的css样式与js的编写代码!

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