IntersectionObserver's callback function was not called
P粉052724364
2023-08-15 20:15:03
<p>I have a cross viewer. Everything works fine. I have a top section. But when I scroll down and then refresh (it refreshes to the previously visible section), the cross-viewer callback is called to the top section instead of the visible section. </p>
<pre class="brush:php;toolbar:false;">const observerCallBack = (entries, observer) => {
const [entry] = entries
console.log("Callback:",entry.target)
if(!entry.isIntersecting) return;
if(entry.target.children.length > 1){
entry.target.children[1].classList.remove('slide-from-right')
entry.target.children[0].classList.remove('slide-from-left')
}else{
entry.target.classList.remove('slide-from-left')
}
observer.unobserve(entry.target)
}
const observeOptions = {
root:null,
threshold:[0.5, 0.9],
}
const observer = new IntersectionObserver(observerCallBack,observeOptions)</pre>
<p>I've tried using console.log but didn't find any solution. </p>
You need to subscribe to the observer you are creating by providing it with the element you want to observe.