I created a basic side navigation menu. When you resize the window to a smaller size, a red square will appear. If you click on this red square, the menu will open.
The menu opens correctly, but I want it to close when I click on the dark area instead of the X button. I tried adding a "click" event listener to the body and removing the "is-open" class, but that didn't work. After spending some time troubleshooting, I decided to seek suggestions here.
"use strict";
const menuToggle = document.querySelector(".menu-toggle");
const menuClose = document.querySelector(".menu-close");
const nav = menuToggle.parentElement;
menuToggle.addEventListener("click", event => {
event.preventDefault();
nav.classList.add("is-open");
document.body.style.backgroundColor = "rgba(0,0,0,0.5)";
});
menuClose.addEventListener("click", event => {
event.preventDefault();
menuToggle.nextElementSibling.style.width = null;
document.body.style.backgroundColor = null;
nav.classList.remove("is-open");
});
:root {
box-sizing: border-box;
}
*, *::before, *::after {
box-sizing: inherit;
}
body {
margin: 0;
padding: 0;
}
.menu-toggle {
width: 40px;
height: 40px;
border: 1px solid red;
cursor: pointer;
}
.menu-container {
position: absolute;
background: lightskyblue;
height: 100vh;
width: 0;
transition: width 0.4s ease-in-out;
top: 0;
left: 0;
overflow: auto;
z-index: 1;
}
.menu-close {
position: absolute;
right: 1em;
}
.nav-menu {
list-style: none;
padding-left: 0;
margin: 50px 0 0 0;
}
.nav-menu > li + li {
border-top: 1px solid #fff;
}
.nav-menu > li > a {
display: block;
color: #000;
padding: 0.8em 1em;
font-size: 1.1rem;
text-decoration: none;
text-transform: uppercase;
}
.nav.is-open .menu-container {
width: 200px;
}
.menu-close::before {
content: "\00d7";
font-size: 2.6rem;
}
/*@media screen and (min-width: 37.5em) {*/
@media screen and (min-width: 40.5em) {
body {
background: #fff !important;
}
.menu-toggle {
display: none;
}
.nav.is-open .menu-container {
width: auto;
height: auto;
}
.menu-container {
position: initial;
height: auto;
width: auto;
overflow: hidden;
}
.menu-close {
display: none;
}
.nav-menu {
display: flex;
position: static;
justify-content: center;
margin: 0;
}
.nav-menu > li {
margin-left: 1em;
}
.nav-menu > li + li {
border-top: initial;
}
}
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="responsive.css">
<title>Responsive Navigation Menu</title>
</head>
<body>
<nav class="nav">
<div class="menu-toggle">
<span class="menu-toggle__linecenter"></span>
</div>
<div class="menu-container">
<span class="menu-close"></span>
<ul class="nav-menu">
<li><a href="#">Home</a></li>
<li><a href="#">Menu Item 1</a></li>
<li><a href="#">Menu Item 2</a></li>
<li><a href="#">Menu Item 3</a></li>
</ul>
</div>
</nav>
<main>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit.
Accusamus accusantium aliquid consequatur facere illum
incidunt magnam magni maiores nam neque numquam omnis
perferendis porro quae quibusdam, quos sed tenetur ullam.
</p>
</main>
<script src="responsible.js"></script>
</body>
</html>