简体   繁体   English

如何通过鼠标悬停使div元素可见/隐藏

[英]How to make a div-Element be visible/hidden by Mouseover

I have three DIV-Elements in a row within a php-File and i want to hide and show the DIV in the middle (Box2) by onMouseOver. 我在php文件中连续有三个DIV元素,我想通过onMouseOver隐藏和显示中间(Box2)的DIV。

echo"<div class='colorBox' id='box1'>Box1</div>";
echo"<div class='colorBox' id='box2'>Box2</div>";
echo"<div class='colorBox' id='box3'>Box3</div>";

I came to two options how to solve this, but they are not working. 我谈到了两种解决方案,但是它们没有用。 Fisrt i tried to solve it with pure CSS: Fisrt我试图用纯CSS解决它:

#box2:hover
{
    visibility: hidden;
}

But when i use this the DIV starts flickering while moving the curser over the div. 但是,当我使用此按钮时,DIV开始闪烁,同时将光标移动到div上。

Second i tried to solve it with javascript and to set an onmouseover and an onmouseout -Event to the div like this 其次,我试图用javascript解决它,并设置div这样的onmouseoveronmouseout -Event到div

CSS: CSS:

echo"<div class='colorBox' id='box2' onmouseover='hideBox()' onmouseout='showBox()'> Box2 </div>";

Javascript: Javascript:

function hideBox()
{
    var box2 = document.getElementById("box2");
        box2.style.visibility = "hidden";
}

function showBox()
{
    var box2 = document.getElementById("box2");
    box2.style.visibility = "visible";
}

But this solution makes the DIV flickering too. 但是这种解决方案也使DIV闪烁。 I found out other solutions with display:none but this is messing up my content very bad. 我发现了display:none其他解决方案,但这使我的内容非常混乱。

I hope you can me help out of this. 希望您能为您提供帮助。 Any help is highly apreciated. 非常感谢您的帮助。

Your code is actually removing the box causing the flickering, you mouse over, the element is gone and there for the mouse is no longer over it causing it to display again. 您的代码实际上是删除导致闪烁的框,将鼠标悬停在该元素上,该元素不再存在,并且鼠标不再位于其上,从而使其再次显示。

You can fix this by using opacity 您可以使用opacity来解决此问题

#box2:hover
{
    opacity: 0;
}

Here is a solution; 这是一个解决方案; by making a parent <div> your problem will be solved: 通过将父级设为<div>您的问题将得到解决:

 .box1head:hover #box1 { visibility: hidden } 
 <div class="box1head"> <div class='colorBox' id='box1'>Box1</div> </div> <div class='colorBox' id='box2'>Box2</div> <div class='colorBox' id='box3'>Box3</div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM