-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
32 lines (24 loc) · 997 Bytes
/
script.js
File metadata and controls
32 lines (24 loc) · 997 Bytes
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
document.addEventListener('DOMContentLoaded', function () {
const sections = document.querySelectorAll('section');
const navLinks = document.querySelectorAll('nav ul li a');
function changeActiveClass() {
let index = sections.length;
while (--index && window.scrollY + 50 < sections[index].offsetTop) { }
navLinks.forEach((link) => link.classList.remove('active'));
navLinks[index].classList.add('active');
}
changeActiveClass();
window.addEventListener('scroll', changeActiveClass);
});
// Function to check scroll position and toggle 'active' class on back to top button
function checkScroll() {
const backToTop = document.querySelector('.back-to-top');
const scrollPosition = window.scrollY || document.documentElement.scrollTop;
if (scrollPosition > 500) {
backToTop.style.display = 'block';
} else {
backToTop.style.display = 'none';
}
}
// Attach the function to the window's scroll event
window.addEventListener('scroll', checkScroll);