Skip to content

Commit 2bf0337

Browse files
authored
Only play video's when they are in viewport (#673)
1 parent 398cc44 commit 2bf0337

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

docs/src/components/Feature.astro

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,9 @@ if (asset.alignment === "full-width") {
172172
/>
173173
) : (
174174
<video
175-
class="image"
175+
class="image js-feature-video"
176176
src={asset.path}
177177
aria-label={asset.alt}
178-
autoplay
179178
muted
180179
playsinline
181180
loop

docs/src/components/Features.astro

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,26 @@ const features = await Promise.all(
4040
))
4141
}
4242
</section>
43+
44+
<script>
45+
// Play videos when they are in view, pause when they are not
46+
const videos = document.querySelectorAll(".js-feature-video");
47+
48+
const observer = new IntersectionObserver(
49+
(entries) => {
50+
entries.forEach((entry) => {
51+
const videoElement = entry.target as HTMLVideoElement;
52+
if (entry.isIntersecting) {
53+
videoElement.play();
54+
} else {
55+
videoElement.pause();
56+
}
57+
});
58+
},
59+
{ threshold: 1 },
60+
);
61+
62+
videos.forEach((video) => {
63+
observer.observe(video);
64+
});
65+
</script>

0 commit comments

Comments
 (0)