我一直在努力使我的导航栏在按下汉堡菜单时向下移动。我添加了CSS并进行了研究,但是我一直在努力理解为什么它不起作用。
我希望导航栏下拉并更改链接的不透明度。
我使用下面的答案来适应我的代码,但仍然遇到困难。
提前致谢。
function myFunction() {
var x = document.getElementById("myLinks");
if(x.className.indexOf('easein') > -1) {
x.classList.remove('easein');
x.classList.add('easeout')
}
else {
x.classList.add('easein');
x.classList.remove('easeout')
}
}
.topnav {
display: block;
background-color: #fff;
box-shadow: 1px 1px 16px 2px red;
position: fixed;
z-index: 45;
right: 0;
width: 100vw;
}
.topnav a#home {
left: 0;
}
.topnav .myLinks {
height: 0;
opacity: 0;
transition: all 1s ease-in-out;
-webkit-transition: all 1s ease-in-out;
-moz-transition: all 1s ease-in-out;
}
.topnav a {
color: #0000a0;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
font-weight: 700;
display: block;
}
.topnav a.icon {
background: #fff;
display: block;
position: absolute;
right: 0;
top: 4vw;
text-align: center;
}
.topnav a:hover {
background-color: #ddd;
color: black;
}
.fa {
color: red;
}
#myLinks a{
left: -10%;
border-radius: 0;
width: 100vw;
margin: 0;
}
.topnav .myLinks.easein {
height:500px;
opacity: 1;
}
.topnav .myLinks.easeout {
height:0px;
opacity: 0;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<div class="topnav">
<a href="#home" class="online">Logo</a>
<div id="myLinks" class="myLinks">
<a href="#news">Portal</a>
<a href="#contact">Feedback</a>
<a href="#about">Logout</a>
</div>
<a href="javascript:void(0);" class="icon" onclick="myFunction()">
<i class="fa fa-bars"></i>
</a>
</div>
最佳答案
问题是您已将CSS属性显示设置为具有动画,我建议您将其更改为opacity
和height
,还请注意,我使用classList来添加/删除类,这是实现的功能您正在寻找:
function myFunction() {
var x = document.getElementById("myLinks");
if(x.className.indexOf('easein') > -1) {
x.classList.remove('easein');
x.classList.add('easeout')
}
else {
x.classList.add('easein');
x.classList.remove('easeout')
}
}
.topnav {
background-color: #fff;
box-shadow: 1px 1px 16px 2px red;
position: fixed;
z-index: 20;
right: 0;
width: 100vw;
}
.topnav a#home {
left: 0;
}
.topnav .myLinks {
height: 0;
opacity: 0;
transition: all 1s ease-in-out;
-webkit-transition: all 1s ease-in-out;
-moz-transition: all 1s ease-in-out;
}
.topnav a {
color: #0000a0;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
display: block;
}
.topnav a.icon {
background: #fff;
display: block;
position: absolute;
right: 0;
top: 4vw;
text-align: center;
}
.topnav a:hover {
background-color: #ddd;
color: black;
}
.fa {
color: red;
}
.myLinks a{
width: 100vw;
}
.topnav .myLinks.easein {
height:500px;
opacity: 1;
}
.topnav .myLinks.easeout {
height:0px;
opacity: 0;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<div class="topnav">
<a href="#home" class="online">Logo</a>
<div id="myLinks" class="myLinks">
<a href="#news">Portal</a>
<a href="#contact">Feedback</a>
<a href="#about">Logout</a>
</div>
<a href="javascript:void(0);" class="icon" onclick="myFunction()">
<i class="fa fa-bars"></i>
</a>
</div>
关于javascript - 努力为导航栏添加下拉效果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61572837/