事件触发函数的问题

dhq314 dhq314 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
以下是我的测试代码,我想实现一种功能就是当层div1被onmouseover时展开div2,div1被onmouseout地收缩div2,但当div2被onmouseover时div2不收缩,只有div1或div2被onmouseout时div2才收缩,但div2被onmouseover时已经onmouseout div1了,即div2由于div1被onmouseout而收缩。我想问怎么设计使div2被onmouseover且div1被onmouseout时,层div2不会收缩?有没有朋友能给点tips呢?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css">
<!--
#div1{position:relative;height:25px;width:60px;background-color:#FFD5AA;}
#div2{position:absolute;top:25px;left:0px;background-color:#B5FFB5;}
-->
</style>
<script type="text/javascript">
<!--
window.onload=function(){
$("div1").onmouseover=function(){
if ($("div2").style.width != "0px") return;
$("div2").style.display = "block";
expand($("div2"),"+",140,"block");
}
$("div1").onmouseout=function(){
if ($("div2").style.width != "140px") return;
expand($("div2"),"-",0,"none");
}
/*$("div2").onmouseover=function(){
if (stop){
clearTimeout(stop);
}
}*/
}
function expand(o,fuhao,wh,dis){
eval("var th = parseInt(o.style.height)"+fuhao+"5");
eval("var tw = parseInt(o.style.width)"+fuhao+"5");
o.style.height = th+"px";
o.style.width = tw+"px";
stop = setTimeout(function(){ expand(o,fuhao,wh,dis) },10);
if (th == wh || tw == wh){
clearTimeout(stop);
o.style.display = dis;
}
}
function $(oid){
return document.getElementById(oid);
}
//-->
</script>
</head>
<body>
<div id="div1">
<div id="div2" style="width:0px;height:0px;display:none;"></div>
</div>
</body>
</html>

...全文

相关推荐

评论 15
收藏 已收藏
分享
返回顶部
dhq314
JavaScript 87,983 成员 224,681 内容