Skip to content

Instantly share code, notes, and snippets.

@webag
Created October 30, 2020 08:27
Show Gist options
  • Save webag/168d3096581b89116a939b19b00c266a to your computer and use it in GitHub Desktop.
Save webag/168d3096581b89116a939b19b00c266a to your computer and use it in GitHub Desktop.
Intersection ovserver video play pause
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