So I have tried to create a hamburger menu and have faced no luck. Whenever I click on the menu icon, it changes to an ‘X’ however it does not open the menu. I have been trying for the last couple of days, and have also researched but have found nothing that works.
Here is my HTML code for the navmenu and hamburger icon:
let menu = document.querySelector('#menu-icon');
let navmenu = document.querySelector('.navmenu');
menu.onclick = () => {
menu.classList.toggle('bx-x');
navmenu.classList.toggle('open');
}
.navmenu {
display: flex;
}
.navmenu a {
color: #000;
font-size: 16px;
text-transform: capitalize;
padding: 10px 20px;
font-weight: 400;
transition: all .42s ease;
}
.navmenu a:hover {
color: #03A9F4;
}
.nav-icon {
display: flex;
align-items: center;
}
.nav-icon i {
margin-right: 20px;
color: #ffffff;
font-size: 25px;
font-weight: 400;
transition: all .42s ease;
}
.nav-icon i:hover {
transform: scale(1.1);
color: #03A9F4;
}
#menu-icon {
font-size: 35px;
display: none;
color: #ffffff;
z-index: 10001;
cursor: pointer;
}
@media(max-width: 750px) {
#menu-icon {
display: block;
}
.navmenu {
position: absolute;
top: -100%;
right: 0%;
width: 300px;
height: 130vh;
background: #000;
display: flex;
flex-direction: column;
align-items: center;
padding: 120px 30px;
transition: all .42s;
}
.navmenu a {
display: block;
margin: 10px 0;
}
.navmenu.open {
right: 0%
}
}
<a href="#" class="logo"><img src="images/logo5.png" alt="" class="prevent-select"></a>
<ul class="navmenu">
<li><a href="#" id="prevent-select" class="hover-underline-animation">Home</a></li>
<li><a href="#" id="prevent-select" class="hover-underline-animation">Shop</a></li>
<li><a href="#" class="hover-underline-animation" id="prevent-select">Sale</a></li>
<li><a href="#" class="hover-underline-animation" id="prevent-select">Contact</a></li>
<li><a href="#" class="hover-underline-animation" id="prevent-select">Policies</a></li>
<li><a href="#" class="hover-underline-animation" id="prevent-select">More</a></li>
</ul>
<div class="nav-icon">
<a href="#" id="prevent-select"><i class='bx bx-search-alt-2'></i></a>
<div class="dropdown2">
<button class="dropbtn2">
<a href="#" id="prevent-select"><i class='bx bx-user'></i></a>
</button>
<div class="dropdown-content2">
<div class="login">
<ul>
<a href="login_register.php" id="prevent-select">Log in/Register</a>
</ul>
</div>
</div>
</div>
<a href="#" id="prevent-select"><i class='bx bx-shopping-bag'></i></a>
<div class="bx bx-menu" id="menu-icon"></div>
</div>
I tried this but it did not work as it had resulted in what I had mentioned above. I basically want it to slide the menu open when I click the hamburger menu icon.
2
Answers
You can use this as an example
@MohammedMalek’s is a little better than this one, but another example, just in case you’re interested
You can also google around and find CSS-only solutions to this problem – I personally find the JS version much easier to deal with, but I’ll throw it out there anyways.