gpt4 book ai didi

html - 悬停错误元素时触发纯 CSS 下拉菜单

转载 作者:行者123 更新时间:2023-11-28 12:46:40 25 4
gpt4 key购买 nike

所以我有一个纯 css 下拉菜单,但我遇到了一个问题。当您将鼠标悬停在链接上时,下拉菜单会起作用,但当您将鼠标悬停在包含下拉菜单部分的实际容器上时,它也会触发。这是带有代码的jsfiddle。 http://jsfiddle.net/9BRac/

<div class="nav-con">
<nav role='navigation' id="nav">
<ul>
<li id="linked"><a href="#">Home</a></li>
<li><a href="#">About</a>
<ul class="dropdown">
<li>Link 1.</li>
<li>Link 2.</li>
<li>Link 3</li>
</ul>
</li>
<li><a href="#">Clients</a>
<ul class="dropdown">
<li>Link 1.</li>
<li>Link 2.</li>
<li>Link 3</li>
</ul>
</li>
<li><a href="#">Contact Us</a>
<ul class="dropdown">
<li>Link 1.</li>
<li>Link 2.</li>
<li>Link 3</li>
</ul>
</li>
</ul>
</nav>


.clearfix:before,
.clearfix:after {
content: " ";
display: table;
}
.clearfix:after {
clear: both;
}
.clearfix {
*zoom: 1;
}

nav {
width: 100%;
height: 60px;
background-color:black;
border-bottom: #ffd600 solid 10px;
margin: 0 auto;
}

nav ul {
position:inheirt;
}

nav ul li {
transition: all .6s ease-in-out;
-moz-transition: all .6s ease-in-out;
-o-transition: all .6s ease-in-out;
-webkit-transition: all .6s ease-in-out;
width: 80px;
height:60px;
margin-left:10px;
display:;
float: left;
line-height: 60px;
text-align:center;
list-style:none;
position:inherit;
color: white;
text-decoration:none;
}

nav ul li:hover {
width: 80px;
-moz-transition: all .6s ease-in-out;
-o-transition: all .6s ease-in-out;
-webkit-transition: all .6s ease-in-out;
height:60px;
margin-left:10px;
background-color:#ffd600;
float: left;
line-height: 60px;
text-align:center;
list-style:none;
position:inherit;
color: white;
text-decoration:none;
}

nav ul li a {
color: white;
text-decoration:none;
}

html {
background-color: #f2f2f2;
}

#dropdown {
width:100%;
height:200px;
opacity: 0.8;
background-color:black;
display:none;
}


.dropdown {
margin-top:10px;

}
.dropdown li {
width: 300px;
background-color: #ffd600;
}

nav ul ul {
opacity: 0;
transition: all .6s ease-in-out;
-moz-transition: all .6s ease-in-out;
-o-transition: all .6s ease-in-out;
-webkit-transition: all .6s ease-in
}

nav ul li:hover > ul {
opacity: 0.8;
transition: all .6s ease-in-out;
-moz-transition: all .6s ease-in-out;
-o-transition: all .6s ease-in-out;
-webkit-transition: all .6s ease-in-out;
}

nav ul ul li {
opacity: 1.0 !important;
}

nav ul ul li:hover {
width: 350px !important;
}

最佳答案

使用以下内容

/* Your dropdown */
nav ul ul {
opacity: 0;
visibility:hidden;
transition: opacity .6s ease-in-out;
-moz-transition: opacity .6s ease-in-out;
-o-transition: opacity .6s ease-in-out;
-webkit-transition: opacity .6s ease-in
}

/* Code to display your dropdown */
nav ul li:hover > ul {
opacity: 0.8;
visibility:visible;
}

有关工作示例,请转到此处 http://jsfiddle.net/DanielApt/9BRac/12/

解释:您的 .dropdown 仍然存在,它的 opacity0

每当您将鼠标悬停在这个“不可见的”.dropdown 上时,您会触发其父级 li 的悬停样式,这会导致显示 .dropdown再次。

解决方案:为您的.dropdown 设置visibilityhidden,并将visibility 设置为visible 仅当您将鼠标悬停在父 li

上时

也只在opacity属性上使用transition,然后visibility立即改变,但是opacity过渡平滑。

PS:如果你对我为什么不使用 display:none 感到好奇,那是因为你无法转换显示 ( source for the solution using visibiliy

关于html - 悬停错误元素时触发纯 CSS 下拉菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24965367/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com