-
Notifications
You must be signed in to change notification settings - Fork 0
/
isoc.js
51 lines (43 loc) · 1.43 KB
/
isoc.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
let navbarMenu = document.querySelector('.navbar-menu');
let dropdownIsOpen = false;
// Handle dropdown menu toggle
navbarMenu.addEventListener('click', (event) => {
if (event.target.classList.contains('dropdown-toggler')) {
let target = document.querySelector(`#${event.target.dataset.dropdown}`);
if (target) {
if (target.classList.contains('show')) {
target.classList.remove('show');
dropdownIsOpen = false;
} else {
target.classList.add('show');
dropdownIsOpen = true;
}
}
}
});
// Handle closing dropdowns if a user clicks outside
document.body.addEventListener('click', (event) => {
if (dropdownIsOpen) {
navbarMenu.querySelectorAll('.dropdown').forEach((dropdown) => {
let targetIsDropdown = dropdown == event.target;
let clickedOnDropdownToggle = event.target.classList.contains('dropdown-toggler');
if (clickedOnDropdownToggle) {
return;
}
if (!targetIsDropdown && !dropdown.contains(event.target)) {
dropdown.classList.remove('show');
}
});
}
});
// Open links in mobiles
function handleSmallScreens() {
document.querySelector('.navbar-toggler').addEventListener('click', () => {
if (!navbarMenu.classList.contains('active')) {
navbarMenu.classList.add('active');
} else {
navbarMenu.classList.remove('active');
}
});
}
handleSmallScreens();