Created
September 28, 2016 14:40
-
-
Save saginadir/5eaad9cfcd01d33b05ccaa127b06773b to your computer and use it in GitHub Desktop.
CSS animations on the fly generation with JavaScript
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
// 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