JavaScript中延时提示框的思路;

 

首先是布局:

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
div {float: left;margin: 10px;}
#div1 {width: 50px;height: 50px;background: blue;}
#div2 {width: 200px;height: 200px;background: red;display: none;}
</style>
</head>
<body>
<div id="div1"></div>
<div id="div2"></div>
</body>
</html>

 

然后是加事件和东西;

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
div {float: left;margin: 10px;}
#div1 {width: 50px;height: 50px;background: blue;}
#div2 {width: 200px;height: 200px;background: red;display: none;}
</style>
<script>
window.onload=function()
{
var oDiv1=document.getElementById('div1');
var oDiv2=document.getElementById('div2');
var close1=null;

oDiv1.onmouseover=function()
{
clearTimeout(close1);
oDiv2.style.display='block';
};
oDiv1.onmouseout=function()
{
close1=setTimeout(function(){
oDiv2.style.display='none'
},1000);
};
oDiv2.onmouseover=function()
{
clearTimeout(close1);
};
oDiv2.onmouseout=function()
{
close1=setTimeout(function(){
oDiv2.style.display='none'
},1000);
};

}
</script>
</head>
<body>
<div id="div1"></div>
<div id="div2"></div>
</body>
</html>

然后合并可以合并的函数;

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
div {float: left;margin: 10px;}
#div1 {width: 50px;height: 50px;background: blue;}
#div2 {width: 200px;height: 200px;background: red;display: none;}
</style>
<script>
window.onload=function()
{
var oDiv1=document.getElementById('div1');
var oDiv2=document.getElementById('div2');
var close1=null;

oDiv2.onmouseover=oDiv1.onmouseover=function()
{
clearTimeout(close1);
oDiv2.style.display='block';
};
oDiv2.onmouseout=oDiv1.onmouseout=function()
{
close1=setTimeout(function(){
oDiv2.style.display='none'
},1000);
};
}
</script>
</head>
<body>
<div id="div1"></div>
<div id="div2"></div>
</body>
</html>