汉堡不改为X



我正在尝试创建一个汉堡包按钮,单击后该按钮将变为×。一切都很好,我就是不能把×符号做成完美的形状。我想知道我应该翻译多少像素的汉堡菜单的顶部和底部栏。我查了一些教程,它们和我做的一样,但我的一个赛季不起作用。

这是我的代码:

感谢您提前提供的帮助。

const navSlide = () => {
const hamburger = document.querySelector(".hamburger");
const nav = document.querySelector(".nav-links");
const navLinks = document.querySelectorAll(".nav-links li");
hamburger.addEventListener("click", () => {
//Toggle
nav.classList.toggle("nav-active");
//Animate
navLinks.forEach((link, index) => {
if (link.style.animation) {
link.style.animation = "";
} else {
link.style.animation = `navLinkFade 0.5s ease forwards ${
index / 7 + 0.5
}s`;
}
});
//hamburger animation
hamburger.classList.toggle("toggle");
});
};
navSlide();
* {
margin: 0px;
padding: 0px;
box-sizing: border-box;
}
@import url("https://fonts.googleapis.com/css2?family=Roboto+Mono:ital,wght@1,100&display=swap");
nav {
display: flex;
justify-content: space-around;
align-items: center;
min-height: 8vh;
background-color: white;
font-family: "Roboto Mono", monospace;
}
.logo {
color: #5b3b97;
text-transform: uppercase;
letter-spacing: 0px;
font-size: 20px;
font-family: "Times New Roman", Times, serif;
}
.nav-links {
display: flex;
justify-content: space-between;
width: 30%;
}
.nav-links li {
list-style: none;
padding-left: 20px;
padding-right: 5px;
padding-top: initial;
}
.nav-links a {
color: #5b3b97;
text-decoration: none;
font-weight: bold;
font-size: 18px;
}
.hamburger {
display: none;
cursor: pointer;
}
.hamburger div {
width: 25px;
height: 4px;
background-color: #5b3b97;
margin: 5px;
transition: all 0.3s ease;
}
@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: 10vh;
background-color: rgb(255, 255, 255);
display: flex;
flex-direction: column;
align-items: center;
width: 50%;
transform: translateX(100%);
transition: transform 1s ease-in-out;
}
.nav-links li {
opacity: 0;
}
.hamburger {
display: block;
}
}
.nav-active {
transform: translateX(0%);
}
@keyframes navLinkFade {
from {
opacity: 0;
transform: translateX(40px);
}
to {
opacity: 1.5;
transform: translateX(0px);
}
}
.toggle .line1 {
transform: rotate(45deg);
top: 0;
}
.toggle .line2 {
opacity: 0;
}
.toggle .line3 {
transform: rotate(-45deg);
top: 0;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="style.css" />
<title>Navigation</title>
</head>
<body>
<nav>
<div class="logo">
<h4>Bargain Technologies Pvt.Ltd</h4>
</div>
<ul class="nav-links">
<li><a href="#">Home</a></li>
<li><a href="#">Companies</a></li>
<li><a href="#">Team</a></li>
<li><a href="#">Contact us</a></li>
</ul>
<div class="hamburger">
<div class="line 1"></div>
<div class="line 2"></div>
<div class="line 3"></div>
</div>
</nav>
<script src="app.js"></script>
<div>
</div>
</body>
</html>

class="line 1"中的名称更改为class="line1"。几乎没有什么。。。将您的.hamburger声明为position: relative;,并给出widthheight:

.hamburger {
position: relative;
display: none;
cursor: pointer;
width: 25px;
height: 20px;
}

比,.hamburger div设置为position: absolute:

.hamburger div {
position: absolute;
width: 25px;
height: 4px;
background-color: #5b3b97;
transition: all 0.3s ease;
}

,为每条单行(.hamburger .line1 && .hamburger .line2 && .hamburger .line3(做出正确的位置:

.hamburger .line1 {
top: 0;
}
.hamburger .line2 {
top: 50%;
}
.hamburger .line3 {
top: 100%;
}

最后旋转135度transform: rotate(135deg);,并在设置之前和之后定位顶部,如top: 50%;和中线定义display: none;:

.toggle .line1 {
transform: rotate(-135deg);
top: 50%;
}
.toggle .line2 {
display: none;
}
.toggle .line3 {
transform: rotate(135deg);
top: 50%;
}

相关内容

  • 没有找到相关文章

最新更新