0
Good morning guys, all right with you?
I have a problem that I can not find the solution at all (I am layman with JS yet). The thing is, I have an Offcanvas menu that opens when I click on the burger icon, I can make it open and close, but I wish when I click off the menu it would also close, but I’m not able to do that, when I think of a logic, stop opening and closing the menu. Follow the JS code
const open_menu = document.getElementById('js-open-button');
const close_menu = document.getElementById('js-close-button');
open_menu.addEventListener('click', OnCanvas );
close_menu.addEventListener( 'click', OffCanvas);
var abriu = false;
// Ao clicar no ícone de hambúrguer , abre o menu
function OnCanvas() {
if(!abriu) {
document.body.classList.add('show-menu');
abriu = true;
}
}
// Ao clicar no X fecha o menu
function OffCanvas() {
if(abriu) {
document.body.classList.remove('show-menu');
abriu = false;
}
}
The HTML I’m using
<!-- Off Canvas Menu -->
<div class="offCanvas">
<nav class="menu">
<div class="icon-list">
<!-- Form Search -->
<form class="d-flex">
<input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn search-button-icon" type="submit"><i class="fas fa-search"></i></button>
</form>
<!-- End Form Search -->
<!-- Navbar -->
<ul class="navbar-nav w-100 justify-content-end">
<li class="nav-item">
<a href="#" class="nav-link active" aria-current="page"><i class="fas fa-home"></i> INÍCIO</a>
</li>
</ul>
<!-- End Navbar -->
</div>
</nav>
<button class="close-button" id="js-close-button">Close Menu</button>
</div>
<!-- End Off Canvas Menu -->
<div class="content-wrap">
<div class="js-content">
<header>
<!-- Head Navbar -->
<nav class="navbar navbar-expand-lg bg-header" id="navbarNav">
<div class="container-fluid">
<!-- Toggle Button -->
<button class="menu-button d-block d-xl-none d-lg-none" id="js-open-button" type="button" >
<span><i class="fas fa-bars"></i></span>
</button>
<!-- Toggle Button End -->
<!-- Logo -->
<a class="navbar-brand" href="#">
<img class="img-fluid" width="130" src="./assets/images/logo_imperial.svg" alt="Logo Imperial">
</a>
<!-- End Logo -->
<div class="collapse navbar-collapse">
<!-- Navbar -->
<ul class="navbar-nav w-100 justify-content-end">
<li class="nav-item">
<a href="#" class="nav-link active" aria-current="page">INÍCIO</a>
</li>
</ul>
<!-- End Navbar -->
<!-- Form Search -->
<form class="d-flex" id="search-form">
<input class="form-control me-2" type="search" placeholder="Pesquisar..." aria-label="Pesquisar">
<button class="btn search-button-icon" type="submit"><i class="fas fa-search"></i></button>
</form>
<!-- End Form Search -->
</div>
</div>
</nav>
<!-- End Head Navbar -->
</header>
</div>
</div>
Thanks in advance :)
Top bro, thanks so much!! Just happening a strange problem, on my button to open the menu, I put an icon of Font Awsome, only if I put the class "open-button" on the button, clicking on the icon does not work and vice versa and if I put in both, the icon does not work. You could tell me what’s going on?
– Filipe Ferreira Falco
In the beginning I would say to put in both. It is only a class for control in javascript, it is not intended to associate any css.
– dfvc
Just press "check" at the beginning of your question. Here explains better how to do it.
– dfvc