-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
36 lines (30 loc) · 1.14 KB
/
script.js
File metadata and controls
36 lines (30 loc) · 1.14 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
// Get the navigation bar and the links
const navbar = document.querySelector('.navbar');
const links = document.querySelectorAll('.navbar-links a');
// Add a click event listener to each link
links.forEach(link => {
link.addEventListener('click', () => {
// Remove the "active" class from all links
links.forEach(link => link.classList.remove('active'));
// Add the "active" class to the clicked link
link.classList.add('active');
// Get the target section and scroll to it
const target = document.querySelector(link.hash);
if (target) {
target.scrollIntoView({ behavior: 'smooth' });
}
});
});
// Add a scroll event listener to the window
window.addEventListener('scroll', () => {
// Get the current scroll position
const scrollPosition = window.pageYOffset;
// Check if the hero section is in view
const hero = document.querySelector('.hero');
if (scrollPosition < hero.offsetTop + hero.offsetHeight) {
// Remove the "active" class from all links
links.forEach(link => link.classList.remove('active'));
// Add the "active" class to the first link
links[0].classList.add('active');
}
});