简体   繁体   English

即使在悬停后,悬停在图标上也会使显示可见

[英]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.

相关问题 CSS悬停功能不起作用:将鼠标悬停在图标上时尝试显示说明 - CSS hover is not working: trying to display a description when hovering over an icon 悬停后如何停止图标晃动? - How to stop the icon shakings after hovering over? 将鼠标悬停在元素上会显示第二个元素。 如何在第一个元素悬停后保持第二个元素可见? - Hover over element makes second element appear. How to keep second element visible after hovering off of the first? 将鼠标悬停在另一个对象上时显示图标 - Display icon on hover over another object 将鼠标悬停在按钮上后,悬停过渡不起作用 - Hover transition doesn't work after hovering the mouse over the button 将鼠标悬停在图标上后如何平滑显示带有文本的背景颜色? - How to display the background color with text smoothly after hovering on the icon? div不可见,即使添加display:block之后 - div not visible, even after adding display:block 悬停时淡入DIV-即使悬停在前DIV上 - Fade back DIV on hover- even when hovering over front DIV 为什么 <li> 元素悬停在图像上后将鼠标悬停在元素上时,颜色和宽度不会改变吗? - Why does the <li> element not change color and width when I hover the mouse over it after hovering over the image? 使用html和css在鼠标悬停在图标上方的右侧显示文本 - Display text on right on hover over an icon using html and css
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM