英文:
How to make sibling div element visible when list item is hovered on
问题
I have the following simple header component with navigation items, which when hovered on should add the following two css rules to the mega-menu
div element so that it appears:
visibility: visible;
opacity: 1;
<div class="container">
<ul class="nav-menu">
<li>
<a href="#">Navigation item</a>
</li>
<li>
<a href="#">Navigation item</a>
</li>
<li>
<a href="#">Navigation item</a>
</li>
<li>
<a href="#">Navigation item</a>
</li>
<li>
<a href="#">Navigation item</a>
</li>
</ul>
<div class="mega-menu">
This mega menu should only be visible when one of the list items in the nav menu is hovered on (and then once open will close when no longer hovering on the mega menu)
</div>
</div>
However, as observed through my snippet, I'm struggling to update the mega-menu
class when the a
in the nav-menu
is hovered on. Ideally, the mega-menu should be visible when a list item is hovered on, or the user is hovering on the mega-menu (otherwise, it should not be visible).
Any pointers as to where I'm going wrong would be really appreciated.
英文:
I have the following simple header component with navigation items, which when hovered on should add the following two css rules to the mega-menu
div element so that it appears:
visibility: visible;
opacity: 1;
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.nav-menu {
min-height: 112px;
padding-left: 5.6rem;
padding-right: 5.6rem;
background-color: green;
display: flex;
gap: 24px;
align-items: center;
justify-content: center;
}
.nav-menu li {
list-style: none;
}
.nav-menu li a {
color: white;
text-decoration: none;
}
.nav-menu li a:hover{
text-decoration: underline;
}
.mega-menu {
visibility: hidden;
opacity: 0;
position: absolute;
background-color: lightblue;
width: 100%;
min-height: 250px;
top: 128px;
text-align: center;
}
.nav-menu li a:hover .mega-menu {
visibility: visible;
opacity: 1;
}
<!-- language: lang-html -->
<div class="container">
<ul class="nav-menu">
<li>
<a href="#">Navigation item</a>
</li>
<li>
<a href="#">Navigation item</a>
</li>
<li>
<a href="#">Navigation item</a>
</li>
<li>
<a href="#">Navigation item</a>
</li>
<li>
<a href="#">Navigation item</a>
</li>
</ul>
<div class="mega-menu">
This mega menu should only be visible when one of the list items in the nav
menu is hovered on (and then once open will close when no longer hovering on
the mega menu)
</div>
</div>
<!-- end snippet -->
However, as observed through my snippet, I'm struggling to update the mega-menu
class when the a
in the nav-menu
is hovered on. Ideally the mega-menu should be visible when a list item is hovered on, or the user is hovering on the mega-menu (otherwise it should not be visible).
Any pointers as to where I'm going wrong would be really appreciated.
答案1
得分: 3
这可以在不使用`:has()`(在Firefox中仍然不支持,除非用户明确启用它)和不使用JavaScript的情况下完成。
首先,`.mega-menu`是UL的兄弟元素 - 因此我们使用`.nav-menu:hover + .mega-menu`作为选择器,使其在UL被悬停时可见。
但您可能不希望它在悬停列表本身时就显示,而是只有在悬停在实际导航项目之一时才显示?
这可以通过在UL上应用`pointer-events: none`,然后在LI上再次应用`pointer-events: all`来实现。
```css
.nav-menu {
min-height: 112px;
padding-left: 5.6rem;
padding-right: 5.6rem;
background-color: green;
display: flex;
gap: 24px;
align-items: center;
justify-content: center;
pointer-events: none;
}
.nav-menu li {
list-style: none;
pointer-events: all;
}
.nav-menu li a {
color: white;
text-decoration: none;
}
.nav-menu li a:hover{
text-decoration: underline;
}
.mega-menu {
visibility: hidden;
opacity: 0;
position: absolute;
background-color: lightblue;
width: 100%;
min-height: 250px;
top: 128px;
text-align: center;
}
.nav-menu:hover + .mega-menu {
visibility: visible;
opacity: 1;
}
<div class="container">
<ul class="nav-menu">
<li>
<a href="#">导航项目</a>
</li>
<li>
<a href="#">导航项目</a>
</li>
<li>
<a href="#">导航项目</a>
</li>
<li>
<a href="#">导航项目</a>
</li>
<li>
<a href="#">导航项目</a>
</li>
</ul>
<div class="mega-menu">
当在导航菜单中的列表项上悬停时,此超级菜单才应该可见(然后一旦打开,当不再悬停在超级菜单上时将关闭)。
</div>
</div>
英文:
This can be done without using :has()
(which still has no support in FIrefox, unless the user explicitly enables it), and without JavaScript.
First of all, the .mega-menu
is a sibling of the UL - so we use .nav-menu:hover + .mega-menu
as our selector, to make it visible, when the UL gets hovered.
But you probably don't want it to show already when the list itself gets hovered, but only when one of the actual navigation items gets hovered?
That can be achieved here by putting pointer-events: none
on the UL, and "reversing" that again with pointer-events: all
on the LI.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.nav-menu {
min-height: 112px;
padding-left: 5.6rem;
padding-right: 5.6rem;
background-color: green;
display: flex;
gap: 24px;
align-items: center;
justify-content: center;
pointer-events: none;
}
.nav-menu li {
list-style: none;
pointer-events: all;
}
.nav-menu li a {
color: white;
text-decoration: none;
}
.nav-menu li a:hover{
text-decoration: underline;
}
.mega-menu {
visibility: hidden;
opacity: 0;
position: absolute;
background-color: lightblue;
width: 100%;
min-height: 250px;
top: 128px;
text-align: center;
}
.nav-menu:hover + .mega-menu {
visibility: visible;
opacity: 1;
}
<!-- language: lang-html -->
<div class="container">
<ul class="nav-menu">
<li>
<a href="#">Navigation item</a>
</li>
<li>
<a href="#">Navigation item</a>
</li>
<li>
<a href="#">Navigation item</a>
</li>
<li>
<a href="#">Navigation item</a>
</li>
<li>
<a href="#">Navigation item</a>
</li>
</ul>
<div class="mega-menu">
This mega menu should only be visible when one of the list items in the nav
menu is hovered on (and then once open will close when no longer hovering on
the mega menu)
</div>
</div>
<!-- end snippet -->
答案2
得分: 0
使用仅CSS的一种方法是在通用容器上使用has
选择器,例如:
.container:has(a:hover) .mega-menu{
visibility: visible;
opacity: 1;
}
问题是,目前并非所有浏览器都支持`has`选择器,例如,Firefox尚未实现它。
英文:
One way to do it using CSS only is by using the has
selector on the common container, for example:
.container:has(a:hover) .mega-menu{
visibility: visible;
opacity: 1;
}
The problem is that right now the has
selector is not available on all browsers, Firefox does not implement it yet for example.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论