1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
const menuBtn = document.querySelector(".nav-menu-btn");
const navigation = document.querySelector(".navigation");
menuBtn.addEventListener("click", () => {
navigation.classList.add("active");
// Add listener to element outside the menu
var activeNav = document.querySelector(".navigation.active");
if (activeNav){ // Check if the outside element exists
activeNav.addEventListener("click", (e) => {
// Make sure clicking the menu element won't cause quitting
if(e.target !== e.currentTarget) return;
navigation.classList.remove("active");
});
}
});
|