-
Notifications
You must be signed in to change notification settings - Fork 2
/
navbarToggle.js
27 lines (22 loc) · 947 Bytes
/
navbarToggle.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
// toggles navbar items when hamburger icon is clicked
document.getElementById("hamburger").addEventListener("click", function (e) {
var navItems = document.getElementById("nav-items");
var navbar = document.getElementById("navbar");
// Toggle the visibility of the menu items
navItems.classList.toggle("show");
// Toggle the expansion of the navbar
navbar.classList.toggle("expand");
// Stop the event from propagating to the document click listener
e.stopPropagation();
});
// Close the navbar if the user clicks outside of it
document.addEventListener("click", function (e) {
var navItems = document.getElementById("nav-items");
var navbar = document.getElementById("navbar");
// Check if the navbar is expanded and if the click is outside the navbar
if (navbar.classList.contains("expand") && !navbar.contains(e.target)) {
// Close the navbar
navItems.classList.remove("show");
navbar.classList.remove("expand");
}
});