In my next.js component layout I am trying to check when a modal window that contains the div #snipcart is on visible in the window.
I am trying to use the IntersectionObserver to check this but I'm not getting an output?
How can I check when div #snipcart
on screen in my app?
useEffect(() => {
const cartTotal = document.querySelector('#cartTotal') !== null ? document.querySelector('#cartTotal').textContent.replace('£','') : ''
const cartInvoiceNo = document.querySelector('#invoiceNo') !== null ? document.querySelector('#invoiceNo').textContent : ''
if ((cartTotal &&!cartTotal.includes('}}')) && (cartInvoiceNo && !cartInvoiceNo.includes('}}'))) {
setTagTotal(cartTotal)
setTagInvoiceNo(cartInvoiceNo)
}
let message = document.querySelector('#layout');
// INTERSECTION OBSERVER STUFF
const io = new IntersectionObserver(entries => {
if(entries[0].isIntersecting) {
console.log(entries);
}
}, {
threshold: [0]
});
// ELEMENTS TO OBSERVE
const blk1 = document.querySelector('#snipcart');
// START OBSERVING ELEMENTS
io.observe(blk1);