为什么我不能在侧边栏和导航栏之间切换?
Why can't i toggle between my sidebar and navbar?
我正在左侧制作侧边栏滑块,但它不起作用。我不知道是不是因为我的代码遗漏了某些东西或某处有问题...很高兴你能帮助完成我的代码!非常感谢!
这是我的 html:
<!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">
<title>Document</title>
<link rel="stylesheet" href="style.css">
<script src="https://kit.fontawesome.com/48a972c999.js" crossorigin="anonymous"></script>
</head>
<body>
<div class="navbar">
<i class="fas fa-bars" onclick="active()"></i>
<div class="sidebar">
<i class="fas fa-times" onclick="active()"></i>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
这是我的 css:
.navbar {
background-color: greenyellow;
width: 100%;
height: 100px;
}
.sidebar {
background-color: greenyellow;
height: 100vh;
width: 300px;
left: -100%;
}
.sidebar.active {
background-color: greenyellow;
height: 100vh;
width: 300px;
left: 0;
}
这是我的 JS:
const navbar = document.querySelector('.navbar')
const sidebar = document.querySelector('.sidebar')
function active() {
navbar.addEventListener('click', () => {
navbar.classList.toggle('active')
sidebar.classList.toggle('active')
})
}
1) 你应该使用 addEventListener
而不是 onClick
.
2) 如果您希望导航栏从左侧滑动,那么首先将其设为 left: -100%
,然后在单击按钮后设为 left: 0
.sidebar {
background-color: greenyellow;
height: 100vh;
width: 300px;
position: fixed; /* change */
top: 0; /* change */
bottom: 0; /* change */
left: -100%; /* change */
}
.sidebar.active {
background-color: greenyellow;
height: 100vh;
width: 300px;
left: 0; /* change */
const sidebar = document.querySelector('.sidebar')
const faBars = document.querySelector(".fa-bars")
const faTimes = document.querySelector(".fa-times")
const active = () => sidebar.classList.toggle("active");
faBars.addEventListener("click", active);
faTimes.addEventListener("click", active);
.navbar {
background-color: greenyellow;
width: 100%;
height: 100px;
}
.sidebar {
background-color: greenyellow;
height: 100vh;
width: 300px;
position: fixed;
top: 0;
bottom: 0;
left: -100%;
}
.sidebar.active {
background-color: greenyellow;
height: 100vh;
width: 300px;
left: 0;
}
.fa-bars,
.fa-times {
cursor: pointer;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/fontawesome.min.css" rel="stylesheet"/>
<div class="navbar">
<i class="fas fa-bars"></i>
<div class="sidebar">
<i class="fas fa-times"></i>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</div>
</div>
我正在左侧制作侧边栏滑块,但它不起作用。我不知道是不是因为我的代码遗漏了某些东西或某处有问题...很高兴你能帮助完成我的代码!非常感谢!
这是我的 html:
<!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">
<title>Document</title>
<link rel="stylesheet" href="style.css">
<script src="https://kit.fontawesome.com/48a972c999.js" crossorigin="anonymous"></script>
</head>
<body>
<div class="navbar">
<i class="fas fa-bars" onclick="active()"></i>
<div class="sidebar">
<i class="fas fa-times" onclick="active()"></i>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
这是我的 css:
.navbar {
background-color: greenyellow;
width: 100%;
height: 100px;
}
.sidebar {
background-color: greenyellow;
height: 100vh;
width: 300px;
left: -100%;
}
.sidebar.active {
background-color: greenyellow;
height: 100vh;
width: 300px;
left: 0;
}
这是我的 JS:
const navbar = document.querySelector('.navbar')
const sidebar = document.querySelector('.sidebar')
function active() {
navbar.addEventListener('click', () => {
navbar.classList.toggle('active')
sidebar.classList.toggle('active')
})
}
1) 你应该使用 addEventListener
而不是 onClick
.
2) 如果您希望导航栏从左侧滑动,那么首先将其设为 left: -100%
,然后在单击按钮后设为 left: 0
.sidebar {
background-color: greenyellow;
height: 100vh;
width: 300px;
position: fixed; /* change */
top: 0; /* change */
bottom: 0; /* change */
left: -100%; /* change */
}
.sidebar.active {
background-color: greenyellow;
height: 100vh;
width: 300px;
left: 0; /* change */
const sidebar = document.querySelector('.sidebar')
const faBars = document.querySelector(".fa-bars")
const faTimes = document.querySelector(".fa-times")
const active = () => sidebar.classList.toggle("active");
faBars.addEventListener("click", active);
faTimes.addEventListener("click", active);
.navbar {
background-color: greenyellow;
width: 100%;
height: 100px;
}
.sidebar {
background-color: greenyellow;
height: 100vh;
width: 300px;
position: fixed;
top: 0;
bottom: 0;
left: -100%;
}
.sidebar.active {
background-color: greenyellow;
height: 100vh;
width: 300px;
left: 0;
}
.fa-bars,
.fa-times {
cursor: pointer;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/fontawesome.min.css" rel="stylesheet"/>
<div class="navbar">
<i class="fas fa-bars"></i>
<div class="sidebar">
<i class="fas fa-times"></i>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</div>
</div>