setupObserver = (root, rootMargin) => {
const sectionEls = [...root.querySelectorAll('.section')];
const navEls = [...root.querySelectorAll('.nav > a')];
function observerCallback(entries, observer) {
entries.forEach(entry => {
if (entry.isIntersecting) {
const { index } = entry.target.dataset;
toggleNav(navEls, index);
}
})
}
const options = {
root,
rootMargin,
};
const observer = new IntersectionObserver(observerCallback, options);
sectionEls.forEach(el => observer.observe(el));
}