特效介绍
简单的div+css+js导航带下拉菜单,运行下面的代码查看最终效果:
提示:您可以先修改部分代码再运行
使用方法
1、引入下面的js和css代码:<style type="text/css"> #nav { line-height: 24px; list-style-type: none; background:#666; } #nav a { display: block; width: 80px; text-align:center; } #nav a:link { color:#666; text-decoration:none; } #nav a:visited { color:#666;text-decoration:none; } #nav a:hover { color:#FFF;text-decoration:none;font-weight:bold; } #nav li { float: left; width: 80px; background:#CCC; } #nav li a:hover{ background:#999; } #nav li ul { line-height: 27px; list-style-type: none;text-align:left; left: -999em; width: 180px; position: absolute; } #nav li ul li{ float: left; width: 180px; background: #F6F6F6; } #nav li ul a{ display: block; width: 156px;text-align:left;padding-left:24px; } #nav li ul a:link { color:#666; text-decoration:none; } #nav li ul a:visited { color:#666;text-decoration:none; } #nav li ul a:hover { color:#F3F3F3;text-decoration:none;font-weight:normal; background:#C00; } #nav li:hover ul { left: auto; } #nav li.sfhover ul { left: auto; } #content { clear: left; } </style> <script type=text/javascript> function menuFix() { var sfEls = document.getElementById("nav").getElementsByTagName("li"); for (var i=0; i<sfEls.length; i++) { sfEls[i].onmouseover=function() { this.className+=(this.className.length>0? " ": "") + "sfhover"; } sfEls[i].onMouseDown=function() { this.className+=(this.className.length>0? " ": "") + "sfhover"; } sfEls[i].onMouseUp=function() { this.className+=(this.className.length>0? " ": "") + "sfhover"; } sfEls[i].onmouseout=function() { this.className=this.className.replace(new RegExp("( ?|^)sfhover\\b"), ""); } } } window.onload=menuFix; </script>2、将您的导航用ul布局,最外层ul的id为“#nav”,布局如下代码:
<ul id="nav"> <!--主导航一 开始--> <li> <a href="#">产品介绍</a> <!--下拉菜单 开始--> <ul> <li><a href="#">产品一</a></li> <li><a href="#">产品一</a></li> </ul> </li> <!--主导航二 开始--> <li> <a href="#">服务介绍</a> <!--下拉菜单 开始--> <ul> <li><a href="#">服务二</a></li> <li><a href="#">服务二</a></li> </ul> </li> </ul>