gpt4 book ai didi

javascript - 下拉菜单淡出

转载 作者:太空宇宙 更新时间:2023-11-03 23:00:07 24 4
gpt4 key购买 nike

我创建了一个下拉菜单。当用户将鼠标悬停在导航按钮上时,使用 CSS3 Transitions 类“下拉内容”淡入,如下面的演示所示:

https://jsfiddle.net/4xk78qse/

我想不通的是当用户没有将鼠标悬停在任何按钮上时如何淡出“下拉内容”。

我试过添加

animation fadeout 2s;

@keyframes fadeout {
from { opacity: 1; }
to { opacity: 0; }
}

本质上与 jsfiddle 示例中的动画淡入淡出相反,但这没有用。

总而言之,当用户没有将鼠标悬停在任何按钮上时,我希望淡出。

最佳答案

这里的技巧是使用 CSS Transitions而不是 CSS 动画。您将设置 visibilityopacity 而不是显示。

.dropdown-content {
visibility: hidden;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
-webkit-transition: opacity 2s, visibility 2s;
-moz-transition: opacity 2s, visibility 2s;
-ms-transition: opacity 2s, visibility 2s;
-o-transition: opacity 2s, visibility 2s;
transition: opacity 2s, visibility 2s;
opacity: 0;
}
.dropdown:hover .dropdown-content {
visibility: visible;
opacity: 1;
}

这是您要完成的工作演示:

.dropbtn {
background-color: #222222;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
height: 80px;
font-family: 'Quicksand', sans-serif;
}

.dropdown {
position: relative;
display: inline-block;
}

.dropdown-content {
visibility: hidden;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
-webkit-transition: opacity 2s, visibility 2s;
-moz-transition: opacity 2s, visibility 2s;
-ms-transition: opacity 2s, visibility 2s;
-o-transition: opacity 2s, visibility 2s;
transition: opacity 2s, visibility 2s;
opacity: 0;
}

.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
font-family: 'Quicksand', sans-serif;
}

.dropdown-content a:hover {background-color: #2c2c2c; color: #FFF;}

.dropdown:hover .dropdown-content {
visibility: visible;
opacity: 1;
}

.dropdown:hover .dropbtn {
background-color: #2c2c2c;
}
<div class="dropdown">
<button class="dropbtn">Navigation</button>
<div class="dropdown-content">
<a href="index.html">1</a>
<a href="slimedash.html">2</a>
<a href="gmod.html">3</a>
<a href="minecraft.html">4</a>
<a href="information.html">5</a>
<a href="contact.html">6</a>
</div>
</div>

关于javascript - 下拉菜单淡出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36871635/

24 4 0