The menu icon opens a side navigation on click and the icon turns into a "X". On second click I would like it to close the side navigation and turn back to normal menu icon.
I have the code below:
<!DOCTYPE html>
<html>
<head>
<style>
.sidenav {
height: 100%;
width: 0;
position: fixed;
z-index: 1;
top: 0;
left: 0;
background-color: #111;
overflow-x: hidden;
transition: 0.5s;
padding-top: 60px;
}
.sidenav a {
padding: 8px 8px 8px 32px;
text-decoration: none;
font-size: 25px;
color: #818181;
display: block;
transition: 0.3s;
}
.sidenav a:hover {
color: #f1f1f1;
}
.sidenav .closebtn {
position: absolute;
top: 0;
right: 25px;
font-size: 36px;
margin-left: 50px;
}
#main {
transition: margin-left .5s;
padding: 16px;
}
@media screen and (max-height: 450px) {
.sidenav {padding-top: 15px;}
.sidenav a {font-size: 18px;}
}
.container {
display: inline-block;
cursor: pointer;
}
.bar1, .bar2, .bar3 {
width: 35px;
height: 5px;
background-color: #333;
margin: 6px 0;
transition: 0.4s;
}
.change .bar1 {
-webkit-transform: rotate(-45deg) translate(-9px, 6px) ;
transform: rotate(-45deg) translate(-9px, 6px) ;
}
.change .bar2 {opacity: 0;}
.change .bar3 {
-webkit-transform: rotate(45deg) translate(-8px, -8px) ;
transform: rotate(45deg) translate(-8px, -8px) ;
}
</style>
</head>
<body>
<div id="mySidenav" class="sidenav">
<a href="#">About</a>
<a href="#">Services</a>
<a href="#">Clients</a>
<a href="#">Contact</a>
</div>
<div id="main">
<div class="container" style="font-size:30px;cursor:pointer" onclick="openNav(); myFunction(this)">
<div class="bar1"></div>
<div class="bar2"></div>
<div class="bar3"></div>
</div>
</div>
<script>
function openNav() {
document.getElementById("mySidenav").style.width = "250px";
document.getElementById("main").style.marginLeft = "250px";
}
function closeNav() {
document.getElementById("mySidenav").style.width = "0";
document.getElementById("main").style.marginLeft= "0";
}
function myFunction(x) {
x.classList.toggle("change");
}
</script>
</body>
</html>
It currently opens the side nav fine but I am not sure how to tell it to close the side nav on second click, or where to put my closeNav() funtion.
Any help would be appreciated.
You could use a class toggle. One class could be the menu closed with the x
icon and when you toggle the other class, you can just specify the opposite.
document.getElementById('menu').addEventListener('click', function () { if (this.classList.contains('open')) { this.classList.remove('open'); } else { this.classList.add('open'); } });
#menu { background:red; width:100px; height:200px; } #menu.open { width:250px; background:blue; }
<div id="menu"> <p>click me!</p> </div>
I dislike adding JQuery if it is not already in use...You can do this just by checking the state of the menu.
<div id="mySidenav" class="sidenav">
<a href="#">About</a>
<a href="#">Services</a>
<a href="#">Clients</a>
<a href="#">Contact</a>
</div>
<div id="main">
<div class="container" style="font-size:30px;cursor:pointer" onclick="toggleMenu(this)">
<div class="bar1"></div>
<div class="bar2"></div>
<div class="bar3"></div>
</div>
</div>
<script>
function openNav() {
document.getElementById("mySidenav").style.width = "250px";
document.getElementById("main").style.marginLeft = "250px";
}
function closeNav() {
document.getElementById("mySidenav").style.width = "0";
document.getElementById("main").style.marginLeft= "0";
}
function toggleMenu(x) {
x.classList.toggle("change");
if(document.getElementById("mySidenav").style.width > "1px"){
closeNav();
} else {
openNav();
}
}
</script>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.