-2
I recently followed a tutorial on Youtube regarding a menu that I would really like to use; in responsive mode a hamburger icon is generated, showing the navbar menu when clicked and turning the icon into an X. The navbar menu is closed only when clicked on a menu item or when clicked on the X icon. I would like that in addition to these two forms, the menu also closes when clicked outside the menu field (which has been set to 50vw width). I tried to find a solution in similar reports, but I could not apply in my code.
Follows code base for conference:
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>Website</title>
<link rel="stylesheet" href="/css/style.css">
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Montserrat:ital,wght@0,400;0,700;1,500&display=swap" rel="stylesheet">
</head>
<body>
<header>
<nav>
<a href="/" class="logo">Logotipo</a>
<div class="mobile-menu">
<div class="line1"></div>
<div class="line2"></div>
<div class="line3"></div>
</div>
<ul class="nav-list">
<li><a href="/">Internet</a></li>
<li><a href="/">Soluções</a></li>
<li><a href="/">Portfólio</a></li>
<li><a href="/">Sobre</a></li>
<li><a href="/">Contato</a></li>
</ul>
</nav>
</header>
<main></main>
<script src="/js/mobile-navbar.js"></script>
</body>
</html>
CSS:
* {
margin: 0;
padding: 0;
}
body {
background: #191930;
}
a {
color: #fff;
text-decoration: none;
transition: 0.3s;
}
a:hover {
opacity: 0.7;
}
.logo {
font-size: 24px;
text-transform: uppercase;
letter-spacing: 4px;
}
nav {
display: flex;
justify-content: space-around;
align-items: center;
font-family: system-ui, -apple-system, Helvetica, Arial, sans-serif;
background: #23232e;
height: 8vh;
}
main {
background: url("bg.jpg") no-repeat center center;
background-size: cover;
height: 92vh;
}
.nav-list {
list-style: none;
display: flex;
}
.nav-list li {
letter-spacing: 3px;
margin-left: 32px;
}
.mobile-menu {
display: none;
cursor: pointer;
}
.mobile-menu div {
width: 32px;
height: 2px;
background: #fff;
margin: 8px;
transition: 0.3s;
}
@media (max-width: 999px) {
body {
overflow-x: hidden;
}
.nav-list {
position: absolute;
top: 8vh;
right: 0;
width: 50vw;
height: 92vh;
background: #23232e;
flex-direction: column;
align-items: center;
justify-content: space-around;
transform: translateX(100%);
transition: transform 0.3s ease-in;
}
.nav-list li {
margin-left: 0;
opacity: 0;
}
.mobile-menu {
display: block;
}
}
.nav-list.active {
transform: translateX(0);
}
@keyframes navLinkFade {
from {
opacity: 0;
transform: translateX(50px);
}
to {
opacity: 1;
transform: translateX(0);
}
}
.mobile-menu.active .line1 {
transform: rotate(-45deg) translate(-8px, 8px);
}
.mobile-menu.active .line2 {
opacity: 0;
}
.mobile-menu.active .line3 {
transform: rotate(45deg) translate(-5px, -7px);
}
Javascript:
class MobileNavbar {
constructor(mobileMenu, navList, navLinks) {
this.mobileMenu = document.querySelector(mobileMenu);
this.navList = document.querySelector(navList);
this.navLinks = document.querySelectorAll(navLinks);
this.activeClass = "active";
this.handleClick = this.handleClick.bind(this);
}
animateLinks() {
this.navLinks.forEach((link, index) => {
link.style.animation
? (link.style.animation = "")
: (link.style.animation = `navLinkFade 0.5s ease forwards ${
index / 7 + 0.3
}s`);
});
}
handleClick() {
this.navList.classList.toggle(this.activeClass);
this.mobileMenu.classList.toggle(this.activeClass);
this.animateLinks();
}
addClickEvent() {
this.mobileMenu.addEventListener("click", this.handleClick);
}
init() {
if (this.mobileMenu) {
this.addClickEvent();
}
}
}
const mobileNavbar = new MobileNavbar(
".mobile-menu",
".nav-list",
".nav-list li",
);
mobileNavbar.init();