graphite2 / website /static /js /video-autoplay.js
openfree's picture
Deploy from GitHub repository
2409829 verified
raw
history blame contribute delete
615 Bytes
const VISIBILITY_COVERAGE_FRACTION = 0.25;
window.addEventListener("DOMContentLoaded", () => {
const players = document.querySelectorAll("[data-auto-play]");
players.forEach((player) => {
if (!(player instanceof HTMLVideoElement)) return;
let loaded = false;
new IntersectionObserver((entries) => {
entries.forEach((entry) => {
if (!loaded && entry.intersectionRatio > VISIBILITY_COVERAGE_FRACTION) {
player.removeAttribute("preload");
player.setAttribute("autoplay", "");
loaded = true;
};
});
}, { threshold: VISIBILITY_COVERAGE_FRACTION })
.observe(player);
});
});