简体   繁体   English

onMouseOver和onMouseOut显示和隐藏div

[英]onMouseOver and onMouseOut show and hide div

Ok what I want is when the user moves the mouse pointer over a certain div it should appear. 好的,我想要的是当用户将鼠标指针移到某个div它应该出现。 And when the mouse leaves the div that div should disappear. 当鼠标离开div ,该div应该消失。 This is what I have done so far. 这就是我到目前为止所做的。

<div id="center"  style="position:absolute; left:45%; top:35%;" onMouseOver=" document.getElementById('center').style.visibility = 'visible'" onMouseOut="document.getElementById('center').style.display = 'none'">

But my problem is that when the mouse leaves the div it disappears but when I again go over the div it does not appear. 但是我的问题是,当鼠标离开div它消失了,但是当我再次越过div它没有出现。 How can I fix that ? 我该如何解决?

When you hide the div, you will not be able to mouseover it again. 隐藏div时,将无法再次将鼠标悬停在div上。 That is usually the point of hiding an element, so that clients cannot access it. 通常这就是隐藏元素的要点,以便客户端无法访问它。 One thing you can do is add a container and attach the mouseover event to the container. 您可以做的一件事是添加一个容器,并将mouseover事件附加到该容器。

<div onmouseover="document.getElementById('center').style.visibility = 'visible'">
    <div id="center" onmouseout="this.style.visibility = 'hidden'">
    </div>
</div>

Try like this: 试试这样:

<div id="center"  style="position:absolute; left:45%; top:35%;background-color:#03C;width:400px;height:400px;opacity:0;" onMouseOver="document.getElementById('center').style.opacity = 1" onMouseOut="document.getElementById('center').style.opacity = 0">

I added a background color to the div and some dimension because if the div has nothing inside and no costraints for the dimension it collapse. 我为div和某个尺寸添加了背景色,因为如果div内部没有任何内容且该尺寸没有成本消耗,它就会崩溃。

Hope this is useful 希望这很有用

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

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