Skip to content

Instantly share code, notes, and snippets.

@tylersticka
Created June 17, 2024 17:08
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/accessible-animated-gif-alternatives/
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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment