Last active
November 21, 2024 19:39
-
-
Save tylersticka/ef914b4b3e19e8634568fee9266c16ea to your computer and use it in GitHub Desktop.
Starting point for an animated GIF-like video web component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
More context and resources →