-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
48 lines (39 loc) · 1.47 KB
/
scripts.js
File metadata and controls
48 lines (39 loc) · 1.47 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
// Select elements
let menuIcon = document.querySelector('#menu-icon');
let navbar = document.querySelector('.navbar');
let sections = document.querySelectorAll('section');
let navLinks = document.querySelectorAll('header nav a');
// Scroll event handler
window.onscroll = () => {
// Get current scroll position
let scrollY = window.scrollY;
// Iterate through each section to check visibility
sections.forEach(sec => {
let offset = sec.offsetTop - 150;
let height = sec.offsetHeight;
let id = sec.getAttribute('id');
// Determine if the section is in view
if (scrollY >= offset && scrollY < offset + height) {
// Remove 'active' class from all navLinks
navLinks.forEach(link => {
link.classList.remove('active');
});
// Add 'active' class to the corresponding navLink
document.querySelector('header nav a[href="#' + id + '"]').classList.add('active');
}
});
};
// Menu icon click handler for mobile navigation
/*menuIcon.onclick = () => {
menuIcon.classList.toggle('bx-x');
navbar.classList.toggle('active');
};*/
document.addEventListener('DOMContentLoaded', () => {
// Your JavaScript code here
let menuIcon = document.querySelector('#menu-icon');
let navbar = document.querySelector('.navbar');
menuIcon.onclick = () => {
menuIcon.classList.toggle('bx-x');
navbar.classList.toggle('active');
};
});