gpt4 book ai didi

javascript - 单击汉堡菜单后看不到我的文字

转载 作者:太空宇宙 更新时间:2023-11-03 19:54:57 25 4
gpt4 key购买 nike

我有三个文件 index.html、style.css、app.js。

尝试制作响应式布局

我在 .nav-link li 下的 CSS 文件中将不透明度设置为 0。当我点击汉堡图标文本时,我想制作动画,即 文本的不透明度应从 0 变为 1(检查@keframe navLinkFade),然后文本将出现。 但这不会发生。为什么

我已经在 app.js 中正确调用了所有函数,我想请看一下。

当我在 CSS 中将不透明度设置为 1 时,效果很好,但这并没有给我带来淡入淡出的效果。

点击汉堡菜单前的图片:

https://imgur.com/fzye2Hy

点击汉堡菜单后的图片

https://imgur.com/uQwTUOt

index.html

  const navSlide = () => {
const burger = document.querySelector(".burger");
const nav = document.querySelector(".nav-links");
const navLinks = document.querySelectorAll(".nav-links li");

burger.addEventListener("click", () => {
nav.classList.toggle("nav-avtive");

navLinks.forEach((link, index) => {
if (link.style.animation) {
link.style.animation = "";
} else {
link.style.animation = "navLinkFade 0.5s ease forwards ${index / 7}s";
}
});
});
};
navSlide();
  * {
margin: 0px;
padding: 0px;
box-sizing: border-box;
}
nav {
display: flex;
justify-content: space-around;
align-items: center;
min-height: 8vh;
font-family: "Poppins", sans-serif;
background-color: black;
}
.logo {
color: whitesmoke;
text-transform: uppercase;
letter-spacing: 5px;
font-size: 20px;
}

.nav-links {
display: flex;
justify-content: space-around;
width: 30%;
}
.nav-links li {
list-style: none;
}
.nav-links a {
color: rgb(240, 172, 0);
text-decoration: none;
letter-spacing: 3px;
font-weight: bold;
font-size: 14px;
}
.burger {
display: none;
}
.burger div {
background-color: whitesmoke;
height: 3px;
width: 25px;
margin: 5px;
cursor: pointer;
}
@media screen and (max-width: 1024px) {
.nav-links {
width: 60%;
}
}

@media screen and (max-width: 768px) {
body {
overflow-x: hidden;
}
.nav-links {
position: absolute;
right: 0px;
height: 92vh;
top: 8vh;
background-color: black;
display: flex;
flex-direction: column;
align-items: center;
width: 50%;
transform: translateX(100%);
transition: transform 0.5s ease-in;
}
.nav-links li {
opacity: 0;
}
.burger {
display: block;
}
}
.nav-avtive {
transform: translateX(0%);
}

@keyframes navLinkFade {
from {
opacity: 0;
transform: translateX(50px);
}
to {
opacity: 1;
transform: translateX(0px);
}
}
 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<link rel="stylesheet" href="style.css">
<link href="https://fonts.googleapis.com/css?family=Poppins&display=swap" rel="stylesheet">
<title>Document</title>

</head>
<body>
<nav>
<div class="logo">
<h4>The Nav</h4>
</div>
<ul class="nav-links">
<li><a href="#">Home</a></li>
<li><a href="#">About us</a></li>
<li><a href="#">Contact</a></li>
</ul>

<div class="burger">
<div class="line1"></div>
<div class="line2"></div>
<div class="line3"></div>
</div>
</nav>

</div>
<script src="app.js"></script>
</body>
</html>



我希望 burgerMenu 使用 navLinkFade 显示文本

最佳答案

.nav-links li opacity: 0 是主要问题。更改不透明度即可。

@media screen and (max-width: 768px)
.nav-links li {
opacity: 1;
}

对于动画,字符串应该用 ES6 ` 包裹起来,而不是双引号。因为双引号内的逻辑计算或变量不起作用。双引号或单引号内 ${index/7} 被视为字符串。

link.style.animation = `navLinkFade 0.5s ease forwards ${index / 7}s`

关于javascript - 单击汉堡菜单后看不到我的文字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57051880/

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