-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
46 lines (40 loc) · 1.63 KB
/
script.js
File metadata and controls
46 lines (40 loc) · 1.63 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
document.addEventListener('DOMContentLoaded', () => {
const hamburger = document.getElementById('hamburger-menu');
const sidebar = document.getElementById('sidebar');
const navLinks = document.querySelectorAll('.nav-link');
// Toggle sidebar on hamburger click
hamburger.addEventListener('click', () => {
sidebar.classList.toggle('active');
hamburger.classList.toggle('active');
});
// Close sidebar when a navigation link is clicked (for mobile)
navLinks.forEach(link => {
link.addEventListener('click', () => {
if (window.innerWidth <= 992) { // Only close on mobile sizes
sidebar.classList.remove('active');
hamburger.classList.remove('active');
}
});
});
// Add active class to current section in sidebar nav
const sections = document.querySelectorAll('section');
const mainNavLi = document.querySelectorAll('.main-nav li a');
function activateNavLink() {
let current = '';
sections.forEach(section => {
const sectionTop = section.offsetTop;
const sectionHeight = section.clientHeight;
if (pageYOffset >= sectionTop - sectionHeight / 3) { // Adjust offset as needed
current = section.getAttribute('id');
}
});
mainNavLi.forEach(a => {
a.classList.remove('active');
if (a.href.includes(current)) {
a.classList.add('active');
}
});
}
window.addEventListener('scroll', activateNavLink);
activateNavLink(); // Call on load to set initial active link
});