我正在尝试根据W3的示例进行下拉菜单。我可以用作下拉菜单,但似乎无法添加过渡。我希望下拉可以放松。
我尝试在.dropdown-content
类中添加transition: all 0.3s;
。我还研究了许多其他下拉示例,但没有一个导致答案。
function myFunction() {
var x = document.getElementById("myTopnav");
if (x.className === "topnav") {
x.className += " responsive";
} else {
x.className = "topnav";
}
}
.dropbtn:hover {
transform: scale(1.02);
color: gray !important;
transition: all 0.5s ease;
}
.topnav a:hover {
transform: scale(1.02);
color: gray;
}
@media screen and (min-width: 769px) {
.topnav {
overflow: hidden;
margin-top: 3em;
padding-top: 0.5em;
display: inline;
float: left;
margin-right: 3.5%;
margin-left: 0;
margin-right: 0;
float: right;
text-align: right;
clear: right;
margin-top: 3em;
padding-top: 1em;
padding-bottom: 0.5em;
}
.topnav a {
float: left;
padding-bottom: 2em;
}
.dropdown:hover .dropdown-content {
display: flex;
border-bottom: 2px solid #d51045;
border-top: 2px solid #d51045;
}
}
@media screen and (max-width: 769px) {
.topnav {
position: relative;
padding-bottom: 1em;
padding-top: 1em;
}
.topnav .icon {
position: absolute;
right: 0;
top: 0;
}
.topnav a {
float: none;
display: block;
text-align: center;
}
.topnav .dropdown {
float: none;
}
.topnav .dropdown-content {
position: relative;
float: none;
text-align: center;
}
.topnav .dropdown .dropbtn {
display: block;
width: 100%;
text-align: center;
}
.dropdown-content a {
float: none !important;
text-align: center !important;
}
.dropdown:hover .dropdown-content {
display: block;
}
}
.topnav a {
display: block;
text-align: center;
padding: 14px 16px;
text-decoration: none;
text-transform: uppercase;
font-weight: 600;
font-size: 14px;
color: #444444;
}
.topnav .icon {
display: none;
}
.dropdown {
float: left;
overflow: hidden;
}
.dropdown .dropbtn {
border: none;
outline: none;
padding: 14px 16px;
background-color: white;
font-family: inherit;
margin: 0;
text-transform: uppercase;
font-weight: 600;
font-size: 14px;
color: #444444;
transition: color .2s ease-in;
}
.dropdown-content {
display: none;
/*box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);*/
z-index: 1;
background-color: white;
position: fixed;
top: auto;
right: 0;
width: 100%;
border-bottom: 2px solid #d51045;
border-top: 2px solid #d51045;
}
.dropdown-content a {
float: right;
padding: 20px 23px;
text-decoration: none;
display: block;
text-align: left;
margin: 0 auto;
}
body.home .site-header .container {
padding-bottom: 1em;
}
img.nav-img {
display: block;
margin-left: auto;
margin-right: auto;
}
<div class="topnav" id="myTopnav">
<a href="#home" class="active"> <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e0/SNice.svg/220px-SNice.svg.png" height="22" class="nav-img">Calendar</a>
<div class="dropdown">
<button class="dropbtn"> <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e0/SNice.svg/220px-SNice.svg.png" height="22" class="nav-img">About
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
<a href="#">Musicians</a>
<a href="#">More</a>
</div>
</div>
<div class="dropdown">
<button class="dropbtn"> <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e0/SNice.svg/220px-SNice.svg.png" height="22" class="nav-img">Tickets
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
<a href="#">Series Subscriptions</a>
<a href="#">More</a>
</div>
</div>
<div class="dropdown">
<button class="dropbtn"> <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e0/SNice.svg/220px-SNice.svg.png" height="22" class="nav-img">Community
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
<a href="#">Learning Programs</a>
</div>
</div>
<div class="dropdown">
<button class="dropbtn"> <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e0/SNice.svg/220px-SNice.svg.png" height="22" class="nav-img">Visit
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
<a href="#">Seating Charts</a>
<a href="#">Frequently Asked Questions</a>
</div>
</div>
<div class="dropdown">
<button class="dropbtn"><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e0/SNice.svg/220px-SNice.svg.png" height="22" class="nav-img">Give
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
<a href="#">Ways to Give</a>
<a href="#">More</a>
</div>
</div>
<a href="#" class="active"><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e0/SNice.svg/220px-SNice.svg.png" height="22" class="nav-img">Shop</a>
<a href="javascript:void(0);" style="font-size:15px;" class="icon" onclick="myFunction()">☰</a>
</div>
我希望下拉列表具有平稳的过渡,类似于laphil.org菜单。
问题在CSS显示属性中。您想动画下拉的外观,但不能使用显示器进行动画。相反,我建议您使用可见性和不透明度。
.dropdown-content {
display: flex;
visibility: hidden;
opacity: 0;
}
.dropdown:hover .dropdown-content {
/*display: flex;*/
visibility: visible;
opacity: 1;
transition: all 0.3s linear;
}