我创建了一个简单的下拉菜单,只要我将鼠标悬停在父列表项上,就会缩放和淡入,"服务"。除了当我将鼠标悬停在子元素(下拉菜单)上时,所有内容都可以正常工作。我知道简单地用不透明度隐藏它不会阻止用户将鼠标悬停在它上面但它允许0.4s过渡生效。如果我用display:none或visibility:hidden来隐藏它,那么转换就会立即发生。我考虑使用jQuery来解决这个问题,但是想知道是否有纯粹基于CSS的解决方案。
* {
margin: 0;
padding: 0;
font-family: Montserrat;
}
li {
list-style: none;
}
a {
text-decoration: none;
}
nav {
background-color: white;
height: 60px;
display: flex;
justify-content: center;
align-items: center;
}
nav > ul > li {
display: inline-block;
position: relative;
height: 60px;
cursor: pointer;
}
nav a {
color: rgba(165,175,185,1);
}
nav > ul > li > a {
padding: 0 20px;
height: 100%;
line-height: 60px;
}
nav > ul > li:hover > a {
color: rgba(105,115,125,1);
}
nav > ul > li:first-of-type {
margin-right: auto;
}
.services-list-container {
position: absolute;
width: 200px;
top: 100%;
height: auto;
opacity: 0;
transform: rotateX(-20deg) scale(0.9,0.9);
transform-origin: 0 0;
transition: transform 0.4s ease, opacity 0.4s ease;
}
.services-list-container ul {
background-color: white;
box-shadow: 0 6px 16px rgba(40, 40, 90, 0.15);
position: relative;
margin-top: 30px;
}
.services-list-container a {
padding: 20px;
display: block;
}
.services-list-container a:hover {
padding: 20px;
display: block;
background-color: rgba(235,240,245,1);
}
.hover:hover .services-list-container {
transform: rotateX(0deg) scale(1,1);
opacity: 1;
transition: transform 0.4s ease, opacity 0.4s ease;
}

<nav>
<ul>
<li>
<a href="#">[Logo]</a>
</li>
<li>
<a href="#">About</a>
</li>
<li class="hover">
<a href="#">Services</a>
<div class="services-list-container">
<ul>
<li>
<a href="#">Information</a>
</li>
<li>
<a href="#">Technology</a>
</li>
<li>
<a href="#">Training</a>
</li>
<li>
<a href="#">Project Support</a>
</li>
</ul>
</div>
</li>
<li>
<a href="#">News</a>
</li>
<li>
<a href="#">Jobs</a>
</li>
<li>
<a href="#">Contact</a>
</li>
</ul>
</nav>
&#13;
答案 0 :(得分:0)
请检查这是否是您想要的。隐藏子菜单,直到悬停发生。
* {
margin: 0;
padding: 0;
font-family: Montserrat;
}
li {
list-style: none;
}
a {
text-decoration: none;
}
nav {
background-color: white;
height: 60px;
display: flex;
justify-content: center;
align-items: center;
}
nav>ul>li {
display: inline-block;
position: relative;
height: 60px;
cursor: pointer;
}
nav a {
color: rgba(165, 175, 185, 1);
}
nav>ul>li>a {
padding: 0 20px;
line-height: 60px;
}
nav>ul>li:hover>a {
color: rgba(105, 115, 125, 1);
}
nav>ul>li:hover .services-list-container {
display: inline-block;
}
nav>ul>li:first-of-type {
margin-right: auto;
}
.services-list-container {
visibility: hidden;
z-index: -1;
position: absolute;
width: 200px;
top: 100%;
opacity: 0;
transition: opacity .4s ease;
}
.services-list-container ul {
background-color: white;
box-shadow: 0 6px 16px rgba(40, 40, 90, 0.15);
position: relative;
}
.services-list-container a {
padding: 20px;
display: block;
}
.services-list-container a:hover {
background-color: rgba(235, 240, 245, 1);
}
.hover:hover .services-list-container {
z-index: 0;
visibility: visible;
opacity: 1;
}
&#13;
<nav>
<ul>
<li>
<a href="#">[Logo]</a>
</li>
<li>
<a href="#">About</a>
</li>
<li class="hover">
<a href="#">Services</a>
<div class="services-list-container">
<ul>
<li>
<a href="#">Information</a>
</li>
<li>
<a href="#">Technology</a>
</li>
<li>
<a href="#">Training</a>
</li>
<li>
<a href="#">Project Support</a>
</li>
</ul>
</div>
</li>
<li>
<a href="#">News</a>
</li>
<li>
<a href="#">Jobs</a>
</li>
<li>
<a href="#">Contact</a>
</li>
</ul>
&#13;
答案 1 :(得分:0)
并不是你在子元素上面盘旋,而是你的列表元素从高处如此高:60px,当你将鼠标悬停在单词之下时,它仍然会越过li元素。
答案 2 :(得分:0)
如果您定位支持浏览器,则可以使用pointer-events来阻止子菜单导致悬停鼠标事件
将元素pointer-events:none
设为禁用
.services-list-container {
pointer-events:none;
position: absolute;
width: 200px;
top: 100%;
height: auto;
opacity: 0;
transform: rotateX(-20deg) scale(0.9,0.9);
transform-origin: 0 0;
transition: transform 0.4s ease, opacity 0.4s ease;
}
然后为了确保您仍然可以进入子菜单,请在您的.hover元素悬停时重置pointer-events
.hover:hover .services-list-container {
pointer-events:all;
transform: rotateX(0deg) scale(1,1);
opacity: 1;
transition: transform 0.4s ease, opacity 0.4s ease;
}
演示
* {
margin: 0;
padding: 0;
font-family: Montserrat;
}
li {
list-style: none;
}
a {
text-decoration: none;
}
nav {
background-color: white;
height: 60px;
display: flex;
justify-content: center;
align-items: center;
}
nav > ul > li {
display: inline-block;
position: relative;
height: 60px;
cursor: pointer;
}
nav a {
color: rgba(165,175,185,1);
}
nav > ul > li > a {
padding: 0 20px;
height: 100%;
line-height: 60px;
}
nav > ul > li:hover > a {
color: rgba(105,115,125,1);
}
nav > ul > li:first-of-type {
margin-right: auto;
}
.services-list-container {
pointer-events:none;
position: absolute;
width: 200px;
top: 100%;
height: auto;
opacity: 0;
transform: rotateX(-20deg) scale(0.9,0.9);
transform-origin: 0 0;
transition: transform 0.4s ease, opacity 0.4s ease;
}
.services-list-container ul {
background-color: white;
box-shadow: 0 6px 16px rgba(40, 40, 90, 0.15);
position: relative;
margin-top: 30px;
}
.services-list-container a {
padding: 20px;
display: block;
}
.services-list-container a:hover {
padding: 20px;
display: block;
background-color: rgba(235,240,245,1);
}
.hover:hover .services-list-container {
pointer-events:all;
transform: rotateX(0deg) scale(1,1);
opacity: 1;
transition: transform 0.4s ease, opacity 0.4s ease;
}
&#13;
<nav>
<ul>
<li>
<a href="#">[Logo]</a>
</li>
<li>
<a href="#">About</a>
</li>
<li class="hover">
<a href="#">Services</a>
<div class="services-list-container">
<ul>
<li>
<a href="#">Information</a>
</li>
<li>
<a href="#">Technology</a>
</li>
<li>
<a href="#">Training</a>
</li>
<li>
<a href="#">Project Support</a>
</li>
</ul>
</div>
</li>
<li>
<a href="#">News</a>
</li>
<li>
<a href="#">Jobs</a>
</li>
<li>
<a href="#">Contact</a>
</li>
</ul>
</nav>
&#13;
答案 3 :(得分:0)
可见性的组合:隐藏;使用不透明度更改可以帮助您保留该动画,同时仍然阻止它在预期的菜单区域之外触发:
https://jsfiddle.net/4wg4sbqy/ CSS:
* {
margin: 0;
padding: 0;
font-family: Montserrat;
}
li {
list-style: none;
}
a {
text-decoration: none;
}
nav {
background-color: white;
height: 60px;
display: flex;
justify-content: center;
align-items: center;
}
nav > ul > li {
display: inline-block;
position: relative;
height: 60px;
cursor: pointer;
}
nav a {
color: rgba(165,175,185,1);
}
nav > ul > li > a {
padding: 0 20px;
height: 100%;
line-height: 60px;
}
nav > ul > li:hover > a {
color: rgba(105,115,125,1);
}
nav > ul > li:first-of-type {
margin-right: auto;
}
.services-list-container {
position: absolute;
width: 200px;
top: 100%;
height: auto;
opacity: 0;
visibility: hidden;
transform: rotateX(-20deg) scale(0.9,0.9);
transform-origin: 0 0;
transition: transform 0.4s ease, opacity 0.4s ease;
}
.services-list-container ul {
background-color: white;
box-shadow: 0 6px 16px rgba(40, 40, 90, 0.15);
position: relative;
margin-top: 30px;
}
.services-list-container a {
padding: 20px;
display: block;
}
.services-list-container a:hover {
padding: 20px;
display: block;
background-color: rgba(235,240,245,1);
}
.hover:hover .services-list-container {
visibility: visible;
transform: rotateX(0deg) scale(1,1);
opacity: 1;
transition: transform 0.4s ease, opacity 0.4s ease;
}
HTML:
<nav>
<ul>
<li>
<a href="#">[Logo]</a>
</li>
<li>
<a href="#">About</a>
</li>
<li class="hover">
<a href="#">Services</a>
<div class="services-list-container">
<ul>
<li>
<a href="#">Information</a>
</li>
<li>
<a href="#">Technology</a>
</li>
<li>
<a href="#">Training</a>
</li>
<li>
<a href="#">Project Support</a>
</li>
</ul>
</div>
</li>
<li>
<a href="#">News</a>
</li>
<li>
<a href="#">Jobs</a>
</li>
<li>
<a href="#">Contact</a>
</li>
</ul>
</nav>