-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Expand file tree
/
Copy pathscripts.js
More file actions
48 lines (41 loc) · 1.7 KB
/
Copy pathscripts.js
File metadata and controls
48 lines (41 loc) · 1.7 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
//
// Scripts
//
window.addEventListener('DOMContentLoaded', event => {
// Activate Bootstrap scrollspy on the main nav element
const sideNav = document.body.querySelector('#sideNav');
if (sideNav) {
new bootstrap.ScrollSpy(document.body, {
target: '#sideNav',
rootMargin: '0px 0px -40%',
});
};
// Collapse responsive navbar when toggler is visible
const navbarToggler = document.body.querySelector('.navbar-toggler');
const responsiveNavItems = [].slice.call(
document.querySelectorAll('#navbarResponsive .nav-link')
);
responsiveNavItems.map(function (responsiveNavItem) {
responsiveNavItem.addEventListener('click', () => {
if (window.getComputedStyle(navbarToggler).display !== 'none') {
navbarToggler.click();
}
});
});
// Close navbar when clicking outside of it (accessibility improvement)
const navbarCollapse = document.body.querySelector('#navbarResponsive');
if (navbarToggler && navbarCollapse) {
document.addEventListener('click', (event) => {
// Check if navbar is expanded and toggler is visible (mobile view)
const isNavbarExpanded = navbarCollapse.classList.contains('show');
const isTogglerVisible = window.getComputedStyle(navbarToggler).display !== 'none';
if (isNavbarExpanded && isTogglerVisible) {
// Check if click is outside the navbar
const isClickInsideNav = sideNav.contains(event.target);
if (!isClickInsideNav) {
navbarToggler.click();
}
}
});
}
});