Codepen: https://codepen.io/vvaciej/pen/abPdEBN
Don’t know how to do a function that closes a menu when active by right click outside
const menuButton = document.querySelector('.fa-bars');
const barsArea = document.querySelector('.bars-area');
const bodyArea = document.querySelector('body');
menuButton.addEventListener('click', function () {
if (!barsArea.classList.contains('active')) {
barsArea.classList.add('active');
} else if (barsArea.classList.contains('active')) {
barsArea.classList.remove('active');
}
});
I tried this:
bodyArea.addEventListener('click', function() {
if (barsArea.classList.contains('active') {
barsArea.classList.remove('active');
{
});
But it didn’t work. So I ask you guys maybe somebody knows how to do it right!!!??
2
Answers
You have to change
bodyArea.addEventListener
todocument.addEventListener
. Starting withbarsArea.classList.contains('active')
is a good beginning, andbarsArea
does not have a target!barsArea.contains(e.target)
and the target has not to be amenuButton
. so that you can remove the active one like you didbarsArea.classList.remove('active');
:you can add a new div say bodyArea to have home content, make height, width as 100vh and add a click listener to it and remove the active class.