Skip to content

Instantly share code, notes, and snippets.

@tylersticka
Last active November 21, 2024 19:39
Show Gist options
  • Save tylersticka/ef914b4b3e19e8634568fee9266c16ea to your computer and use it in GitHub Desktop.
Save tylersticka/ef914b4b3e19e8634568fee9266c16ea to your computer and use it in GitHub Desktop.
Starting point for an animated GIF-like video web component
// https://cloudfour.com/thinks/video-gifs-are-forever-lets-make-them-better/
class GifLike extends HTMLElement {
static motionQuery = window.matchMedia(
"(prefers-reduced-motion: no-preference)"
);
connectedCallback() {
this.video = this.querySelector("video");
GifLike.motionQuery.addEventListener("change", (query) => {
this.toggle(query.matches);
});
this.toggle(GifLike.motionQuery.matches);
}
toggle(state) {
if (state) {
this.video.play();
} else {
this.video.pause();
}
}
}
customElements.define("gif-like", GifLike);
@tylersticka
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment