汉堡不换成X
Hamburger not changing to 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;
并给出 width
和 height
:
.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%;
}
我正在尝试创建一个汉堡包按钮,点击后会变成 ×
。一切正常,我只是无法使 ×
标志完美无缺。我想知道我应该翻译汉堡菜单顶部和底部栏的多少像素。我查了一些教程,他们做的和我做的一样,但由于某些原因我的不工作。
这是我的代码:
提前感谢您的帮助。
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;
并给出 width
和 height
:
.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%;
}