【发布时间】:2011-06-19 18:08:46
【问题描述】:
我有一个简单的水平菜单,有四个选项。 当我将鼠标悬停在一个 div(例如 div A)上时,它的子项出现并且 当我将鼠标移出特定的 div 时,它的子元素就会消失。
我已经为鼠标悬停放置了一个 setTimeOut 函数(大约是 300)。
在某些特定情况下,我想禁用 setTimeout
1. 当我 mouseout div A 和 i mouseover div B 时,我会不那个延迟, 我只想展示 B 的 childDiv
2. 当我 mouseout div B 和我 mouseover div C 时,我会不那个延迟, 我只想显示 C 的 childDiv
但是我怎样才能做到这一点呢?
只是我有一系列事件: (一个简单的算法)
If(mouseout(divA) && mouseover(divB))
{
disable setTimeOut;
show(ChildDivB); //with no delay
}
else If(mouseout(divB) && mouseover(divC))
{
disable setTimeOut;
show(ChildDivC); //with no delay
}
}
一般情况下,当 i mouseover && mouseout 在 "foo" div 中时,(foo 是包含所有 div 的 div)settimeout 会被禁用。
这可以在jquery中完成吗?
我的代码如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
.myDiv {
border:1px solid red;
height:30px;
width:100px;
float:left;
}
.myDivChild {
display:none;
width:300px;
height:300px;
}
</style>
</head>
<body>
<div class="foo" style="background-color:#3300FF;width:900px;height:30px;margin-top:100px;">
<div class="myDiv" style="background-color:red;margin-left:100px">A
<div class="myDivChild" style="background-color:red">
Child Div A
</div>
</div>
<div class="myDiv" style="background-color:yellow">B
<div class="myDivChild" style="background-color:yellow">
Child Div B
</div>
</div>
<div class="myDiv" style="background-color:green">C
<div class="myDivChild" style="background-color:green">
Child Div C
</div>
</div>
<div class="myDiv" style="background-color:#00CCCC">D
<div class="myDivChild" style="background-color:#00CCCC">
Child Div D
</div>
</div>
</div>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
var interval;
$('.foo > div').hover(function(){
var $showDiv = $(this).find('div');
interval = setTimeout(function(){
$showDiv.show();
},300);
},
function(){
clearTimeout(interval);
$(this).find('div').hide();
});
</script>
</body>
</html>
【问题讨论】:
-
这个问题解决了吗?
标签: javascript settimeout jquery