如何实现导航元素的过渡效果?



我想让我的导航部分更漂亮一点。目前,当我将鼠标悬停在菜单项上以显示其中的页面时,页面会立即显示出来。我希望他们逐渐出现,而不是一下子全部出现。我试过在我的CSS文件中nav ul li:hover ul下面添加transition: 2s;,但这似乎不起作用。

header {
color: #348899;
background-color: #343642;
background-image: url(Images/heading.png);
background-position: right;
background-repeat: no-repeat;
}
#wrapper {
width: 90%;
margin-right: auto;
margin-left: auto;
}
h1 {
margin: 0;
padding: 10px;
font-size: 42pt;
line-height: 36pt;
font-weight: lighter;
}
h2 {
margin: 0;
padding: 0 10px 5px;
font-size: 18pt;
line-height: 22pt;
font-weight: bold;
font-style: italic;
}
h3 {
margin: 0;
padding: 0 10px 5px 0;
font-size: 18pt;
line-height: 22pt;
font-weight: bold;
font-style: italic;
}
nav {
color: #343642;
background-color: #979C9C;
float: left;
width: 160px;
font-weight: bold;
height: 100%;
position: relative;
}
nav ul li {
list-style: none;
padding: 10px;
}
nav ul li a {
display: block;
text-decoration: none;
color: #343642;
}
nav ul li ul {
display: none;
position: relative;
left: auto;
right: 35%;
}
nav ul li:hover ul  {
display: block;
transition: 2s;
}
nav ul li ul li:hover {
background-color: #b1b6b6;
width: 80px;
}
nav ul li ul li a:hover {
color: #fff;
}
nav ul li a:hover {
color: #fff;
background-color: #b1b6b6;
}
main {
color: #17354A;
background-color: #F2EBC7;
margin-left: 160px;
padding: 10px;
z-index: 0;
display: flex;
}
.column  {
float: left;
}
.left {
width: 75%;
}
.right {
width: 25%;
}
.text {
font-size: 25px;
margin: 0 20px 0 0;
}
main .row .left {
width: 25%;
}
main .row .left p {
margin-right: 5px;
}
main .row .middle {
width: 50%;
}
main .row .middle h3 {
text-align: center;
}
main .row .right {
width: 25%;
}
main .side .left {
width: 50%;
}
main .side .right {
width: 50%;
}
.brass {
float: right;
margin: 0 0 5px 5px;
}
.logo {
float: right;
margin: 0 0 5px 5px;
}
body{
background-color: #348899;
font-family: Calibri, Helvetica, Arial, Sans-Serif;
}
footer {
color: #348899;
background-color: #343642;
font-style: italic;
text-align: center;
margin-left: 160px;
}
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="project.css">
<meta charset="utf-8" />
<title>Beginning Band Players - Home</title>
</head>
<body>
<div id="wrapper">
<header>
<h1>
Beginning Band Players
</h1>
<h2>
Home
</h2>
</header>
<nav>
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="#">Brass</a>
<ul>
<li><a href="Brass/trumpet.html">Trumpet</a></li>
<li><a href="Brass/frenchhorn.html">Horn</a></li>
<li><a href="Brass/trombone.html">Trombone</a></li>
<li><a href="Brass/euphonium.html">Euphonium</a></li>
<li><a href="Brass/tuba.html">Tuba</a></li>
</ul>
</li>
<li><a href="#">Woodwind</a>
<ul>
<li><a href="Woodwind/clarinet.html">Clarinet</a></li>
<li><a href="Woodwind/flute.html">Flute</a></li>
<li><a href="Woodwind/oboe.html">Oboe</a></li>
<li><a href="Woodwind/saxophone.html">Saxophone</a></li>
</ul>
</li>
<li><a href="#">Percussion</a>
<ul>
<li><a href="Percussion/bells.html">Bells</a></li>
<li><a href="Percussion/snare.html">Snare Drum</a></li>
</ul>
</li>
<li><a href="misc/maintenence.html">Maintenence</a></li>
<li><a href="misc/additional-equipment.html">Additional Equipment</a></li>
</ul>
</nav>
<main>
<div class="column left">
<div class="logo">
<img src="Images/Logo-E.png" alt="logo" height="200" width="355">
</div>
<h3>
About us:
</h3>
<div class="text">
<p>
The purpose of Beginning Band Players is to provide students and parents
with the necessary resources for a successful start in any band program.
It's our hope that prospective music students find what they're looking
for and enjoy a lifetime of music.
</p>
<div class="brass">
<img src="Images/brass-1.jpg" alt="brass" height="238" width="425">
</div>
</div>
</div>
<div class="column right">
<h3>
What to Expect:
</h3>
<p>
Students can learn a little about each instrument found in beginning band
programs and decide which one they like best. They'll be able to see and
hear what each instrument sounds like.
</p>
<h3>
Additional Resources:
</h3>
<p>
Students may also need additional equipment such as practice books or supplies
to help maintain their instruments. Links to such supplies can be found on the
Additional Equipment page.
</p>
</div>
</main>
<footer>
Copyright &copy; 2022, Carter Thomas Wolfe <br>
Web Project Prototype
</footer>
</div>
</body>
</html>

您必须告诉CSS您想要转换哪个属性,以及您希望如何转换。如果你把过渡行改成:

transition: opacity 2s ease-in;

也就是说:在超过2秒的时间内淡出不透明度,并加上"easing"。函数。https://developer.mozilla.org/en-US/docs/Web/CSS/easing-function上面这条线应该可以工作-但只有当它有不同的不透明状态之间转换。元素的默认不透明度为1,所以简单地定义不透明度的转换是不够的,你还必须在不同的状态之间进行更改。如果你使用不透明度,我会尝试将元素/类/任何东西的默认状态设置为0,并将悬停状态设置为1。

.classYouWantToFadeIn {
opacity:0;
/*rest of the CSS*/
}
.classYouWantToFadeIn:hover {
opacity: 1;
}

如果转换仍然不起作用,请阅读"特异性规则"。在CSS。https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity

试试这个:

nav ul li:hover ul  {
display: block;
transition: 2s ease-in-out;
}

最新更新