Created
October 30, 2020 08:27
-
-
Save webag/168d3096581b89116a939b19b00c266a to your computer and use it in GitHub Desktop.
Intersection ovserver video play pause
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const videos = document.querySelectorAll("video"); | |
const config = { | |
rootMargin: '0px -100px', | |
threshold: 0 | |
} | |
const callback = (entries) => { | |
entries.forEach((entry) => { | |
if (!entry.isIntersecting) { | |
entry.target.pause(); | |
console.log('Exit video'); | |
} else { | |
entry.target.play(); | |
console.log('Enter video'); | |
} | |
}); | |
} | |
const observer = new IntersectionObserver(callback, config); | |
videos.forEach(element => { | |
observer.observe(element); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment