這是最近一個(gè)項(xiàng)目使用到的下拉菜單效果,首先定義一個(gè)id為nav的ul,ul->li里面放的是下拉出來的菜單,也是一個(gè)ul:
<ul id="nav">
<li><a href="<?url ctrl=home?>">首 頁</a></li>
<li><a href="#">|</a></li>
<li><a href="#">關(guān)于我們</a>
<!-- 下拉部分-->
<ul><li><a href="#">公司簡(jiǎn)介</a></li>
<li><a href="#">總經(jīng)理致辭</a></li>
</ul></li>
<!-- 下拉部分-->
</ul>
js就幾行代碼:
<script type=text/javascript><!--//--><![CDATA[//><!--
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>
css代碼:
#nav {
line-height: 24px; list-style-type: none;
}
#nav a {
display: block; text-align:center;
}
#nav a:link {
color:#a6ce38; text-decoration:none;
}
#nav a:visited {
color:#a6ce38;text-decoration:none;
}
#nav a:hover {
color:#F00; text-decoration:none;;
}
#nav li {
float: left; padding-left:15px; font-family:"微軟雅黑"; font-size:14px;
}
#nav li a:hover{
color:#F00;
}
#nav li ul {
line-height:18px; list-style-type: none;text-align:left;
left: -999em; width:120px; position: absolute;
}
#nav li ul li{
background: #FFF;/*沒有背景在ie6下會(huì)有一點(diǎn)小問題*/
float:left; width:100px;
font-family:"微軟雅黑"; font-size:12px; color:#F00;
margin:0px;padding:0;
clear:both;
}
#nav li ul a{
display: block; width:100px;text-align:left;
}
#nav li ul a:link {
color:#a6ce38; text-decoration:none;
}
#nav li ul a:visited {
color:#a6ce38; text-decoration:none;
}
#nav li ul a:hover {
color:#FFF; text-decoration:none;
background:#F00;
}
#nav li:hover ul {
left: auto;
}
#nav li.sfhover ul {
left: auto;
}