function show(ali,aul){
var timer=null;
ali.onmouseover=function(){
clearTimeout(timer);
this.style.background="red";
aul.style.display='block';
}
ali.onmouseout=function(){
var This=this;
timer=setTimeout(function(){
This.style.background="blue";
aul.style.display='none'
},80)
}
aul.onmouseover=function(){
clearTimeout(timer);
}
aul.onmouseout=function(){
timer=setTimeout(function(){
aul.style.display="none";
ali.style.background="blue"
},80)
}
}
例子:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>多个延迟菜单</title>
<style type="text/css">
* {margin: 0;padding: 0}
ul {list-style: none;}
#box {width: 505px;margin: 30px auto;position: relative;}
#box #nav {width: 505px;height: 30px;position: absolute;left: 0;top: 0}
#box #nav li {width: 100px;height: 30px;background: blue;text-align: center;line-height: 30px;font-size: 20px;color: #fff;float: left;border-right: 1px solid #fff;cursor: pointer;}
#box #subNav {position: relative;top: 35px;}
#box #subNav ul {position: absolute;display: none;}
#box #subNav ul li {width: 100px;height: 20px;text-align: center;line-height: 20px;padding: 5px;border-bottom: 1px dashed #000;background: #ccc ;cursor: pointer;}
#box #subNav ul li:hover {background: yellow}
</style>
<script type="text/javascript">
window.onload=function(){
var aLi=document.getElementById('nav').getElementsByTagName('li');
var aUl=document.getElementById('subNav').getElementsByTagName('ul');
for (var i = 0; i < aLi.length; i++) {
aUl[i].style.left=i*100+'px';
};
for (var i = 0; i < aLi.length; i++) {
show(aLi[i],aUl[i])
}
function show(ali,aul){
var timer=null;
ali.onmouseover=function(){
clearTimeout(timer);
this.style.background="red";
aul.style.display='block';
}
ali.onmouseout=function(){
var This=this;
timer=setTimeout(function(){
This.style.background="blue";
aul.style.display='none'
},80)
}
aul.onmouseover=function(){
clearTimeout(timer);
}
aul.onmouseout=function(){
timer=setTimeout(function(){
aul.style.display="none";
ali.style.background="blue"
},80)
}
}
}
</script>
</head>
<body>
<div id="box">
<ul id="nav">
<li>产品分类1</li>
<li>产品分类2</li>
<li>产品分类3</li>
<li>产品分类4</li>
<li>产品分类5</li>
</ul>
<div id="subNav">
<ul>
<li>精品男款1</li>
<li>精品女款</li>
<li>精品同款</li>
</ul>
<ul>
<li>精品男款2</li>
<li>精品女款</li>
<li>精品同款</li>
</ul>
<ul>
<li>精品男款3</li>
<li>精品女款</li>
<li>精品同款</li>
</ul>
<ul>
<li>精品男款4</li>
<li>精品女款</li>
<li>精品同款</li>
</ul>
<ul>
<li>精品男款5</li>
<li>精品女款</li>
<li>精品同款</li>
</ul>
</div>
</div>
</body>
</html>