[英]Hover div flickers when rolling mouse over
I have a div that appears when hovering over another div.我有一个 div,当悬停在另一个 div 上时会出现这个 div。
However when I try to move the mouse over the div that appears it flickers.但是,当我尝试将鼠标移到出现的 div 上时,它会闪烁。 I understand this is happening because of my CSS, but can not figure out what I should do to rectify.
我知道这是由于我的 CSS 而发生的,但无法弄清楚我应该做些什么来纠正。
.hoverbase { width: 300px; height: 300px; position: relative; } .hovertop { height: 200px; width: 200px; border: 2px solid red; background-color: white; position: absolute; top: 50px; left: 50px; margin: auto; visibility: hidden; } .hoverbase a:hover+.hovertop { visibility: visible; }
<div class='hoverbase'> <a href=http://www.google.com.au> <img src='https://fakeimg.pl/300x300/949598/888888/' /></a> <div class='hovertop'> <a href=http://google.com.au> <h2>Project HA</h2> </a> </div> </div>
As you seem to be aware, .hovertop
flickers because of quick alternation between hovering and not hovering.正如您似乎知道的那样,
.hovertop
闪烁是因为悬停和不悬停之间的快速交替。 When .hovertop
is visible and the mouse is over it, the :hover
action of .hoverbase
is cancelled and .hovertop
disappears.当
.hovertop
可见且鼠标:hover
在其上时, .hoverbase
的:hover
动作被取消, .hovertop
消失。 At that point, .hovertop
is no longer covering .hoverbase
and the :hover
action is reinstated.那时,
.hovertop
不再覆盖.hoverbase
并且:hover
操作被恢复。 This alternation happens quickly and repeats forever as long as your mouse is over .hovertop
, resulting in a flickering effect.只要您的鼠标悬停在
.hovertop
,这种交替就会发生得很快并且永远重复,从而产生闪烁效果。
Include .hovertop
in your :hover
state selector.在您的
:hover
状态选择器中包含.hovertop
。 That way, it will remain visible whether the mouse is over .hoverbase
or .hovertop
.这样,无论鼠标在
.hoverbase
还是.hovertop
,它都将保持可见。
.hoverbase a:hover + .hovertop,
.hovertop:hover { /* << added this */
visibility:visible;
}
In English, this translates to: If the mouse is over .hoverbase
OR .hovertop
, show .hovertop
.在英语中,这转化为:如果鼠标悬停在
.hoverbase
或.hovertop
,则显示.hovertop
。
Working example:工作示例:
.hoverbase { width: 300px; height: 300px; position: relative; } .hovertop { height: 200px; width: 200px; border: 2px solid red; background-color: white; position: absolute; top: 50px; left: 50px; margin: auto; visibility: hidden; } .hoverbase a:hover+.hovertop, .hovertop:hover { visibility: visible; }
<div class='hoverbase'> <a href=http://www.google.com.au> <img src='//via.placeholder.com/350x150' /></a> <div class='hovertop'> <a href=http://google.com.au> <h2>Project HA</h2> </a> </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.