I'm trying to create the parallax effect using the intersectionObserver API. I want the event listener to be removed from the window object when the block1 section is not intersecting. but it is not letting me remove the event for some reason
const obsCallBack = function (entries) {
entries.forEach(entry => {
let scroll = window.scrollY;
const functionParallex = () => {
let enter = entry;
if (enter.target.id === "block-1") {
let offset = window.scrollY - scroll;
portImg1.style.transform = 'translateY(' + (180 + offset * -0.30) + 'px)';
}
}
if (entry.isIntersecting &&
entry.boundingClientRect.y > 0) {
window.addEventListener('scroll', functionParallex);
}
if (!entry.isIntersecting) {
console.log(entry);
window.removeEventListener('scroll', functionParallex);
}
})
}
const obsOptions = {
root: null,
threshold: 0.01,
}
const observer = new IntersectionObserver(obsCallBack, obsOptions);
observer.observe(block1);