-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
34 lines (29 loc) · 991 Bytes
/
script.js
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
const getId = (link) => link.getAttribute('href').replace('#', '');
const sections = document.querySelectorAll('.section');
const links = document.querySelectorAll('.nav-list__item-link');
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
links.forEach(link => {
link.classList.toggle(
'nav-list__item-link--active',
getId(link) === entry.target.id
);
});
}
});
}, {
threshold: 0.7,
});
const navList = document.querySelector('.nav-list');
sections.forEach(section => observer.observe(section));
navList.addEventListener('click', (e) => {
if (e.target.classList.contains('nav-list__item-link')) {
e.preventDefault();
const id = getId(e.target);
window.scrollTo({
top: document.getElementById(id).offsetTop,
behavior: 'smooth',
});
}
});