初探js之练习js鼠标移入移除时间和练习计时器
2016-03-28 本文已影响585人
gzp1124
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<style type="text/css">
.div1{
width:90px;
background-color: red;
height: 200px;
float:left;
}
.div2{
width:90px;
background-color: yellow;
height: 200px;
float:left;
}
</style>
<script>
var i = 1;
window.onload=function(){
var div1 = document.getElementById("d1");
var div2 = document.getElementById("d2");
var d1T = null;
var d2T = null;
div1.onmouseout=function(){
//500毫秒后隐藏div2
d1T = setTimeout(function(){div2.style.display="none";},500);
}
div1.onmouseover=function(){
//立马显示div2
div2.style.display="";
//清除div2的time
clearTimeout(d2T);
}
div2.onmouseout=function(){
//500毫秒后隐藏div2
d2T = setTimeout(function(){div2.style.display="none";},500);
}
div2.onmouseover=function(){
//进入div2,停掉div1的time
clearTimeout(d1T);
}
}
</script>
</head>
<body>
<div class="div1" id="d1">
这是第一个div
</div>
<div class="div2" id="d2">
这是第二个div
</div>
</body>
</html>
用到的知识点:
- div1.onmouseover=function(){...}鼠标移入时执行
- div2.onmouseout=funciton(){...}鼠标移出
- setTimeout(function(){...},1000);计时器
- clearTimeout(a);清除setTimeout设置的计时器
- div2.style.display="none"隐藏div
- div2.style.display="" 显示div