I have some sections and some navigation links in my page. Im using Intersection Observer to add an active
class to a respective link that redirect to this section. In some part of my page, 2 sections are visibles i want to add the active
class to the most visible section (only once at time) but i don't know how:
Here is the Intersection Observer code:
const sectObserver = new IntersectionObserver(entries => {
for (entry of entries) {
// here i get the section id and the link classList:
const { id } = entry.target,
{ classList } = document.querySelector(`#to-${id}`);
if (entry.intersectionRatio) {
classList.add('active');
} else {
classList.remove('active');
};
};
});
And here the instance of the targets observer:
document.querySelectorAll('section:not(#get-cv, #skills)')
.forEach(link => sectObserver.observe(link));