以下是引用片段: ul li { position: relative; } |
现在我们定义的就是副菜单的内容部分,使用left和top属性我们就可以让它们显示在主菜单内容的右边.display属性值为none所以在默认情况下是隐藏的: 以下是引用片段: li ul { position: absolute; left: 149px; top: 0; display: none; } |
最后得修饰下里面的a元素: 以下是引用片段: ul li a { display: block; text-decoration: none; color: #777; background: #fff; padding: 5px; border: 1px solid #ccc; border-bottom: 0; } |
但因为IE的显示BUG,所以得加上下面这段话进行修复: 以下是引用片段: /* Fix IE. Hide from IE Mac \*/ * html ul li { float: left; } * html ul li a { height: 1%; } /* End */ |
第三部:让它运作起来
我们需要在我们移动到主菜单上时显示副菜单内容: 以下是引用片段: li:hover ul { display: block; } |
好了,你可以测试下代码了,1%人可能会兴奋地叫起来,呵呵遗憾的是就目前这些代码还不能够在IE上运做作出我们想要的结果.要让IE运作出一样的效果,我们得使用一段JS代码,不会非常的烦琐: 以下是引用片段: startList = function() { if (document.all&&document.getElementById) { navRoot = document.getElementById("nav"); for (i=0; i<navRoot.childNodes.length; i++) { node = navRoot.childNodes[i]; if (node.nodeName=="LI") { node.onmouseover=function() { this.className+=" over"; } node.onmouseout=function() { this.className=this.className.replace» (" over", ""); } } } } } window.onload=startList; |
好了,其他代码再做下补充,这个功能就能实现了: 以下是引用片段: li:hover ul, li.over ul { display: block; }
<ul id="nav"> |
自己动手做一个吧,可以更好看些 |