<!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>
<title> new document </title>
<style type="text/css">
#oTip{
width: 120px;
height: 140px;
border: 2px solid #3366FF;
background-color: #FFFEE8;
text-align: center;
display: none;
position: absolute;
font-size: 9pt;
} #oTip li{
line-height: 20px;
}
</style>
<script type="text/javascript">
<!-- // 添加事件
var addEvent = function(a, b, c, d){
if(a.attachEvent) a.attachEvent(b[0], c);
else a.addEventListener(b[1] || b[0].replace(/^on/,""), c, d || false);
return c;
}
// 取得元素位置
var getPosition = function(el){
var itop = el.offsetTop;
var ileft = el.offsetLeft;
while(el = el.offsetParent){
itop += el.offsetTop;
ileft += el.offsetLeft;
}
return {y: itop, x: ileft};
} // 加载时绑定mouseover mouseout事件
window.onload = function(){
var oDiv = document.getElementById("oDiv");
addEvent(oDiv, ["onmouseover"], function(e){
showTips(e)
}); addEvent(oDiv, ["onmouseout"], function(e){
disTips(e)
});
}
// 显示Tips
function showTips(e){
var e = e || window.event;
var otarget = e.srcElement || e.target;
var oTip = document.getElementById("oTip");
oTip.style.display = "block";
oTip.style.position = "absolute";
oTip.style.top = (getPosition(otarget).y + 36) + "px";
oTip.style.left = getPosition(otarget).x + "px";
} // 隐藏Tips
function disTips(e){
var e = e || window.event;
var otarget = e.srcElement || e.target;
var oTip = document.getElementById("oTip");
oTip.style.display = "none";
}
//-->
</script>
</head> <body>
<div id="oDiv" style="width:40px;height:20px;background-Color:#009900;font-size:9pt;">showTips</div> <div id="oTip" >
<ul>
<li><a href="http://www.baidu.com"> www.baidu.com </a></li>
<li><a href="http://www.google.com"> www.google.com </a></li>
<li><a href="http://www.sina.com"> www.sina.com </a></li>
<li><a href="http://www.163.com"> www.163.com </a></li>
</ul>
</div>
</body>
</html>需求:鼠标移到showTips 就显示提示框,移开提示框就隐藏。
问题:但这样导致用户就永远不可能鼠标移入到提示框中进行操作。我希望的是 鼠标移出showTips但没往提示框上移动就隐藏提示框, 但如果鼠标虽然移出showTips 但是移入了提示框中那样提示框
不隐藏 可以进行超链接操作,这时当鼠标再移出提示框的时候 提示框才隐藏。自己怎么想都是个矛盾的,不知道是不是设计的有问题,请大家帮忙
var oTipTimer;// 加载时绑定mouseover mouseout事件
window.onload = function(){
var oDiv = document.getElementById("oDiv");
addEvent(oDiv, ["onmouseover"], function(e){
clearTimeout(oTipTimer);
showTips(e);
});
addEvent(oDiv, ["onmouseout"], function(e){
oTipTimer = setTimeout(function(){
disTips(e);
}, 1000);
});
}
你看看CSDN,你头像上面的提示,他是右边的提示和头像之间有紧密的联系的
但是只有上半端再联系着。
<!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>
<title> new document </title>
<style type="text/css">
#oTip{
width: 120px;
height: 140px;
border: 2px solid #3366FF;
background-color: #FFFEE8;
text-align: center;
display: none;
position: absolute;
font-size: 9pt;
} #oTip li{
line-height: 20px;
}
</style>
<script type="text/javascript">
<!-- // 添加事件
var addEvent = function(a, b, c, d){
if(a.attachEvent) a.attachEvent(b[0], c);
else a.addEventListener(b[1] || b[0].replace(/^on/,""), c, d || false);
return c;
}
// 取得元素位置
var getPosition = function(el){
var itop = el.offsetTop;
var ileft = el.offsetLeft;
while(el = el.offsetParent){
itop += el.offsetTop;
ileft += el.offsetLeft;
}
return {y: itop, x: ileft};
} // 加载时绑定mouseover mouseout事件
window.onload = function(){
var oDiv = document.getElementById("oDiv");
var oTip = document.getElementById("oTip");
addEvent(oDiv, ["onmouseover"], function(e){
showTips(e)
}); addEvent(oDiv, ["onmouseout"], function(e){
disTips(e)
});
addEvent(oTip, ["onmouseout"], function(e){
disTips(e)
});
}
// 显示Tips
function showTips(e){
var e = e || window.event;
var otarget = e.srcElement || e.target;
var oTip = document.getElementById("oTip");
oTip.style.display = "block";
oTip.style.position = "absolute";
oTip.style.top = (getPosition(otarget).y + otarget.offsetHeight) + "px";
oTip.style.left = getPosition(otarget).x + "px";
} // 隐藏Tips
function disTips(e){
var e = e || window.event;
var otarget = e.relatedTarget||e.toElement;
var oTip = document.getElementById("oTip");
if(judge(oTip,otarget))return
oTip.style.display = "none";
}
function judge(parent,child){
if(parent == child) return true;
while(child=child.parentNode){if(child==parent)return true;}
return false;
}
//-->
</script>
</head> <body>
<div id="oDiv" style="width:40px;height:20px;background-Color:#009900;font-size:9pt;">showTips</div>
<div id="oTip" >
<ul>
<li><a href="http://www.baidu.com"> www.baidu.com </a></li>
<li><a href="http://www.google.com"> www.google.com </a></li>
<li><a href="http://www.sina.com"> www.sina.com </a></li>
<li><a href="http://www.163.com"> www.163.com </a></li>
</ul>
</div>
</body>
</html>