[英]Hovering over icon leaves display visible even after hover
I'm trying to make a dropdown menu with selectable links.我正在尝试制作带有可选链接的下拉菜单。 I'm using a gear icon to hover over for a dropdown menu to appear.
我正在使用齿轮图标悬停以显示下拉菜单。
Currently, I have a li
in a ul
as follows:目前,我在
ul
有一个li
,如下所示:
<li id="gear-btn">
<i class="fas fa-cog"></i>
<ul id="gear-dropdown" class="gear-dropdown hidden">
<li>
<ul class="editions">
<span class="dropdown-subtitle">Edition</span>
<li><a href="#">CSS</a></li>
<li><a href="#">HTML</a></li>
<li><a href="#">Javascript</a></li>
<li><a href="#">Ruby</a></li>
</ul>
</li>
<li>
<span class="dropdown-subtitle">Help</span>
<ul class="help">
<li><a href="#">FAQ</a></li>
<li><a href="#">Contact Us</a></li>
</ul>
</li>
</ul>
</li>
and my CSS is:我的 CSS 是:
#gear-btn {
position: relative;
}
#gear-btn:hover .gear-dropdown {
display: block;
}
.gear-dropdown {
position: absolute;
right: 5px;
display: none;
width: 200px;
z-index: 100;
border: 1px solid black;
}
Now what's strange is, if I hover over the gear icon, the menu drops below it, but even when I scroll over the menu, it remains visible until my cursor is off of the .gear-dropdown li
.现在奇怪的是,如果我将鼠标悬停在齿轮图标上,菜单会下降到它下方,但即使我滚动菜单,它仍然可见,直到我的光标离开
.gear-dropdown li
。 This is not what I would expect, since it should only be visible when I'm hovering over the gear icon based on the css (I don't have a .gear-dropdown:hover
selector).这不是我所期望的,因为只有当我将
.gear-dropdown:hover
悬停在基于 css 的齿轮图标上时它才应该可见(我没有.gear-dropdown:hover
选择器)。 It is the desired effect, but I'm trying to learn CSS and figure out why this is happening.这是预期的效果,但我正在尝试学习 CSS 并弄清楚为什么会发生这种情况。
Thanks!谢谢!
This is happening because the mouseover even bubbles up to all the parent elements.这是因为鼠标悬停甚至冒泡到所有父元素。 So when the mouse is over the dropdown, the
hover
is applied for the parent element(#gear-btn) as well, which in turn applies the class #gear-btn:hover .gear-dropdown
.因此,当鼠标
hover
在下拉菜单上时,也将hover
应用于父元素(#gear-btn),进而应用类#gear-btn:hover .gear-dropdown
。 It's not possible to block event bubbling using css.使用 css 无法阻止事件冒泡。
In order to show the dropdown only on hovering the icon, you can either change the class as below:为了仅在悬停图标时显示下拉菜单,您可以更改类,如下所示:
#gear-btn:hover .gear-dropdown:not(:hover) {
display: block
}
Or, as below或者,如下
#gear-btn > i:hover + .gear-dropdown {
display: block;
}
So, you have a cog icon.所以,你有一个齿轮图标。 When you hover over
gear-btn
element it shows a dropdown menu.当您将鼠标悬停在
gear-btn
元素上时,它会显示一个下拉菜单。 Please consider that this whole element - cog icon + dropdown menu have a single parent which is <li id="gear-btn">
.请考虑整个元素 - 齿轮图标 + 下拉菜单有一个父元素
<li id="gear-btn">
。 Even when you hover over its children you are still hovering over <li id="gear-btn">
.即使你将鼠标悬停在它的孩子上,你仍然悬停在
<li id="gear-btn">
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.