I created this function that stops the videos from playing when they are outside the viewport. It doesn't work. Can you help fix it?
const observer = new IntersectionObserver(entries => {
entries.forEach(entry => {
entry.intersectionRatio > 0 ? entry.target.play : entry.target.pause()
})
})
const videos = document.querySelectorAll('video')
videos.forEach(video => {
console.log('video out')
observer.observe(video)
})