-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
64 lines (54 loc) · 1.92 KB
/
Copy pathscript.js
File metadata and controls
64 lines (54 loc) · 1.92 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
// Other important pens.
// Map: https://codepen.io/themustafaomar/pen/ZEGJeZq
// Dashboard: https://codepen.io/themustafaomar/pen/jLMPKm
let dropdowns = document.querySelectorAll('.navbar .dropdown-toggler')
let dropdownIsOpen = false
// Handle dropdown menues
if (dropdowns.length) {
// Usually I don't recommend doing this (adding many event listeners to elements have the same handler)
// Instead use event delegation: https://javascript.info/event-delegation
// Why: https://gomakethings.com/why-event-delegation-is-a-better-way-to-listen-for-events-in-vanilla-js
// But since we only have two dropdowns, no problem with that.
dropdowns.forEach((dropdown) => {
dropdown.addEventListener('click', (event) => {
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 clicked the body
window.addEventListener('mouseup', (event) => {
if (dropdownIsOpen) {
dropdowns.forEach((dropdownButton) => {
let dropdown = document.querySelector(`#${dropdownButton.dataset.dropdown}`)
let targetIsDropdown = dropdown == event.target
if (dropdownButton == event.target) {
return
}
if ((!targetIsDropdown) && (!dropdown.contains(event.target))) {
dropdown.classList.remove('show')
}
})
}
})
// Open links in mobiles
function handleSmallScreens() {
document.querySelector('.navbar-toggler')
.addEventListener('click', () => {
let navbarMenu = document.querySelector('.navbar-menu')
if (navbarMenu.style.display === 'flex') {
navbarMenu.style.display = 'none'
return
}
navbarMenu.style.display = 'flex'
})
}
handleSmallScreens()