setupObserver = (root, rootMargin) => {
const elements = [...root.querySelectorAll('.caption')];
const background = root.querySelector('.background > p');
function observerCallback(entries, observer) {
entries.forEach(entry => {
console.log(entry.target);
if (entry.isIntersecting) {
entry.target.classList.add('active');
background.innerHTML = entry.target.getAttribute('data-index');
} else {
entry.target.classList.remove('active');
}
})
}
const options = {
root,
rootMargin,
};
const observer = new IntersectionObserver(observerCallback, options);
elements.forEach(el => observer.observe(el));
}