<div id="nav">
<ul class="clearfix"><li><a href="javascript:;">站长之家</a>
</li>
<li><a href="javascript:;">行业资讯</a>
<div class="subNav show">
<em></em>
<p>
<span>
<a href="javascript:;">业界动态</a>|
<a href="javascript:;">收购融资</a>|
<a href="javascript:;">门户动态</a>|
<a href="javascript:;">搜索引擎</a>|
<a href="javascript:;">网络游戏</a>|
<a href="javascript:;">电子商务</a>|
<a href="javascript:;">广告传媒</a>|
<a href="javascript:;">厂商开发</a>
</span>
</p>
</div>
</li>
<li><a href="javascript:;">站长在线</a>
<div class="subNav">
<em></em>
<p>
<span>
<a href="javascript:;">站长报道</a>|
<a href="javascript:;">好站推荐</a>|
<a href="javascript:;">站长聚会</a>|
<a href="javascript:;">站长访谈</a>|
<a href="javascript:;">站长茶馆</a>|
<a href="javascript:;">网站排行</a>
</span>
</p>
</div>
</li>
<li><a href="javascript:;">网站运营</a>
<div class="subNav">
<em></em>
<p>
<span>
<a href="javascript:;">建站经验</a>|
<a href="javascript:;">策划盈利</a>|
<a href="javascript:;">搜索优化</a>|
<a href="javascript:;">网站推广</a>|
<a href="javascript:;">免费资源</a>
</span>
</p>
</div>
</li>
<li><a href="javascript:;">设计在线</a>
<div class="subNav">
<em></em>
<p>
<span>
<a href="javascript:;">酷站推荐</a>|
<a href="javascript:;">网页设计</a>|
<a href="javascript:;">WEB标准</a>|
<a href="javascript:;">视频处理</a>|
<a href="javascript:;">设计活动</a>
</span>
</p>
</div>
</li>
<li><a href="javascript:;">网络编程</a>
<div class="subNav">
<em></em>
<p>
<span>
<a href="javascript:;">Asp编程</a>|
<a href="javascript:;">Php编程</a>|
<a href="javascript:;">.Net编程</a>|
<a href="javascript:;">Xml编程</a>|
<a href="javascript:;">Access</a>|
<a href="javascript:;">Mssql</a>|
<a href="javascript:;">Mysql</a>
</span>
</p>
</div>
</li>
<li><a href="javascript:;">联盟资讯</a>
<div class="subNav">
<em></em>
<p>
<span>
<a href="javascript:;">联盟动态</a>|
<a href="javascript:;">联盟介绍</a>|
<a href="javascript:;">联盟点评</a>|
<a href="javascript:;">网赚技巧</a>
</span>
</p>
</div>
</li>
<li><a href="javascript:;" >服务器</a>
<div class="subNav" >
<em></em>
<p>
<span>
<a href="javascript:;">Web服务器</a>|
<a href="javascript:;">Ftp服务器</a>|
<a href="javascript:;">Mail服务器</a>|
<a href="javascript:;">Dns服务器</a>|
<a href="javascript:;">Win服务器</a>|
<a href="javascript:;">Linux服务器</a>|
<a href="javascript:;">安全防护</a>|
<a href="javascript:;">虚拟主机</a>
</span>
</p>
</div>
</li>
</ul>
</div>
<script>
var get = {
byId : function(id){
return document.getElementById(id);
},
byClass: function(sClass,oParent){
var aClass = [];
var reClass = new RegExp("(^| )" + sClass + "( |$)");
var aElem = this.byTagName("*", oParent);
for (var i = 0; i < aElem.length; i++) reClass.test(aElem[i].className) && aClass.push(aElem[i]);
return aClass
},
byTagName: function(elem,obj){
return (obj || document).getElementsByTagName(elem);
}
};
window.onload = function(){
var oNav = get.byId("nav");
var aLi = get.byTagName("li",oNav);
var aSubNav = get.byClass("subNav",oNav);
var oSubNav = oEm = timer = null;
for(var i=0;i<aLi.length;i++){
aLi onmouseover /
aLi[i].onmouseover = function(){
// 先隐藏所有的子菜单
for(var i=0;i<aSubNav.length;i++){aSubNav[i].style.display = "none"};
//找出当前li下面的子菜单,并让他显示
oSubNav = get.byClass("subNav",this)[0];
oEm = get.byTagName("em",this)[0];
oSubNav.style.display = "block";
// 判断子菜单显示的位置
oNav.offsetWidth - this.offsetLeft > oSubNav.offsetWidth ?
//如果在范围内居左显示
oSubNav.style.left = this.offsetLeft + "px" :
//超出范围居右显示
oSubNav.style.right = 0;
//计算指标箭头显示位置( this.offsetLeft - oSubNav.offsetLeft 正好)
oEm.style.left = this.offsetLeft - oSubNav.offsetLeft + 40 + "px";
clearTimeout(timer);
};
aLi onmouseout /
aLi[i].onmouseout = function(){
timer = setTimeout(function(){
oSubNav.style.display = "none";
},500)
};
};
};
</script>
写样式中注意点: 1,子菜单<div class="subNav" >是相对于最外面<div id="nav">的定位,而不是相对于li;
2,小三角 <em>是相对于<div class="subNav" >的定位!
3,子菜单<div class="subNav" > 一定要加 white-space:nowrap,保证他是一行显示,不会被li的宽度所限定!