-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
36 lines (32 loc) · 1.07 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// const bar = document.getElementById("bar");
// const nav = document.getElementById("navbar");
// if (bar) {
// bar.addEventListener("click", () => {
// if (nav.classList.contains("active")) {
// nav.classList.remove("active");
// } else {
// nav.classList.add("active");
// }
// });
// }
const bar = document.getElementById("bar");
const nav = document.getElementById("navbar");
// Function to handle clicks outside the nav list
function handleClickOutside(event) {
if (!nav.contains(event.target) && !bar.contains(event.target)) {
nav.classList.remove("active");
// Remove the event listener to prevent further clicks from closing the menu
document.body.removeEventListener("click", handleClickOutside);
}
}
if (bar) {
bar.addEventListener("click", () => {
if (nav.classList.contains("active")) {
nav.classList.remove("active");
} else {
nav.classList.add("active");
// Add the event listener to detect clicks outside the nav list
document.body.addEventListener("click", handleClickOutside);
}
});
}