How To Use AnimateCSS
What is animate CSS?
Animate CSS is a library of CSS animations made using the keyframe rule, typically used for animating HTML elements / tags - especially text, thus allowing the website to feel much more alive and interactive - there are various animations designed to attract the users attention. They also keep the user amused during loading time - funilly enough, my 2017 - 2018 blog which is (or was depending on when you�re reading this) powered by Hexo uses Animate CSS.
There are a variety of animations and I�m only going to explore a few of them if but you can get a demo of each animation via the AnimateCSS website. You can use it with just about any other tool you like too. (
You can include AnimateCSS in your website using various methods, you can install / download it and place the source code within your project, or you could browse through the source code (on Github) and pick the animations you like and insert only the ones you want into your website (thus you won�t be bloating it up with unused code) - this will mean you can modify the animations to your liking. You can even use a package manager but since I�m feeling lazy I�m just going to include it via CDN. And I will only be using standard syntax CSS so no specified browser support.
Basic HTML Usage
|
|
Here we are simply specifying the class by assigning the class to our desired tag to be animated. Notice we can have some control over the behavior just by specifying a class (such as infinite). This method isn�t very flexible. The class animated just lets AnimateCSS code know that this intends to use an animation from the library.
CSS Usage (and Preprocessor)
Lets first look at a simple animation CSS code to see what is really going. You can look at Animate.css file in the Github repository or via source folder (then the specific file you want to examine). Notice that the animations are categorized - this should help when it comes to picking an appropriate animation.
|
|
All that is really happening here within the keyframe rule is that a time unit is being set to percentage - which means that 10% will be 10% of however long the animation is intended to last for, and 50% is halfway through, and basically at a specific time interval (the percentage) the animation�s position will be changed. The time unit can be something other than percentage but percentage is very flexible. 0% is (from) when the animation is first triggered and 99% is just before (to or until) the animation ends. I plan on doing a tutorial which teaches how to make custom animations which will hopefully better you understanding. The last part - the �swing� class including the animation name essentially just allows us to refer to this animation as �swing�.
|
|
We can simply target a class (or ID), give it an animation belonging to the Animate CSS library and if we like, set its behavior such as the duration (speed) of the animation and so on. And we can even have it so that the element / tag is animated on an event such as a mouse-hover.
|
|
Want one after the other? No problem! I must admit that I tried making a custom keyframe animation which combines some of the animations but it didn�t seem to work. If you�re using a preprocessor you could use some fancy logic to control your web-page liveliness. Similar to how we will do so with Javascript.
Javascript / JQuery Usage
Now for the funnest part, Javascript. Basically the secret here is toggling the class when we want the animation to happen, for example, if the user clicks on an item - we will toggle an element with a class belonging to the AnimateCSS library and then remove it when we feel appropriate (so we can re-toggle it again).
|
|
Don�t forget to comment out / remove the JQuery version if you want the Javascript version vice versa. I�m going to assume to understand what this code (hopefully the comments help) but basically any time we want to do something with animationCSS we simply call our trusty animate (or JqAnimate) function. Theoretically you could extend this to use more than just AnimateCSS. You can then go on to use event handlers, timers and other logic to make your website do all kinds of fancy stuff.
While having knowledge of CSS animations is helpful for using AnimateCSS - as hopefully proven by this tutorial - CSS animation knowledge is not required.