Video 视频在使用IntersectionObserver的视口外暂停
我创建了这个函数,当视频在视口外时,它会阻止视频播放。它不起作用。你能帮我修一下吗Video 视频在使用IntersectionObserver的视口外暂停,video,intersection-observer,Video,Intersection Observer,我创建了这个函数,当视频在视口外时,它会阻止视频播放。它不起作用。你能帮我修一下吗 const observer = new IntersectionObserver(entries => { entries.forEach(entry => { entry.intersectionRatio > 0 ? entry.target.play : entry.target.pause() }) }) const videos = documen
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)
})
也许您已经找到了答案,但实际上并没有调用
.play
方法。您需要像使用.pause()
一样使用.play()
。所以你的第三行应该是:
entry.intersectionRatio > 0 ? entry.target.play() : entry.target.pause()