Div闪烁onmouseover,onmouseout

时间:2014-04-14 12:00:44

标签: javascript html onmouseover onmouseout

我有图像,当鼠标悬停时我想要一个div,但我不希望它在onmouseout上。我编写了代码并且它有效,但它闪烁了。 这是代码:

<div id="div" style="opacity:0.6; background-color:#2c3e50; width:136px; height:163px; z-index:199; position:absolute; vertical-align:top; font-size:120px; text-align:center; color:White; display:none;">1</div>
<img alt="ברק לוי" src="" style="background-color:red; height:163px; width:136px" onmouseover="picture(true)" onmouseout="picture(false)"/>

function picture(bool) {
    if (bool) {
        document.getElementById("div").style.display = "block";
    }
    else if (bool==false) {
        document.getElementById("div").style.display = "none";
    }
}

jsBin 我该如何解决?

3 个答案:

答案 0 :(得分:2)

只需将onmouseover="picture(true)" onmouseout="picture(false)添加到div div。

答案 1 :(得分:0)

因为您需要在onmouseout

上使用div事件
<div id="div" style="opacity:0.6; background-color:#2c3e50; width:136px; height:163px; z-index:199; position:absolute; vertical-align:top; font-size:120px; text-align:center; color:White; display:none;" onmouseout="picture(false)">1</div>

<img alt="ברק לוי" src="" style="background-color:red; height:163px; width:136px" onmouseover="picture(true)"/>

答案 2 :(得分:0)

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
<div id="div" style="opacity:0.6; background-color:#2c3e50; width:136px; height:163px; z-index:199; position:absolute; vertical-align:top; font-size:120px; text-align:center; color:White; display:block;" onmouseover="picture(true)" onmouseout="picture(false)">1
<img alt="" src="http://i.imgur.com/vbKNWRZ.gif" id="image"/></div>
</body>
</html>


function picture(bool) {
    if (bool) {
        document.getElementById("div").style.opacity = "0.6";
    }
    else {
        document.getElementById("div").style.opacity = "0.0";
    }
}

试试这个。闪烁的原因在于,当您设置“显示:无”时,它不存在,并且“鼠标输出”被触发。将不透明度设置为0.0时,它仍然是不可见的,但鼠标输出不会触发。