Skip to content

Instantly share code, notes, and snippets.

@saginadir
Created September 28, 2016 14:40
Show Gist options
  • Save saginadir/5eaad9cfcd01d33b05ccaa127b06773b to your computer and use it in GitHub Desktop.
Save saginadir/5eaad9cfcd01d33b05ccaa127b06773b to your computer and use it in GitHub Desktop.
CSS animations on the fly generation with JavaScript
// We are creating a new style tag
let newStyleSheet = document.createElement("style");
// Appending the style tag to the head.
document.head.appendChild(newStyleSheet);
// Writing to the style sheet some css rules including keyframes and the animation class
newStyleSheet.appendChild(document.createTextNode(`
@keyframes my-fun-keyframes {
/* My keyframes for the animation I want to create */
}
.animate {
animation: my-fun-keyframes 1s infinite;
}
`));
// el is the element we want to animate
el.classList.add("animate");
// Good! now el will animate according to the animation we created above!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment