When it comes to animations in CSS, for our purposes, there are two kinds of animation:
Animation interpolated by the browser between two states.
Animation set by a @keyframes rule.
When it comes to the interactions we have on the websites, we're more likely to use the transition animations, but the keyframes do have their place too.
The transition animations are frankly easier and simpler for us to take advantage of. When setting up your styles for an element, you basically need to set two 'states', and use the transition property to have the computer smartly animate between the two states.
Transition is a shorthand property, containing: transition-delay, transition-duration, transition-property, transition-timing-function.
Not all of these parts of the property are required to work, but two are: transition property, and transition-duration. That's to say, you need to specify the part of an element that is going to be animated, and for how long:
This is referring to what is being animated. You can have certain css properties being animated (like margin, color, background, etc), or have any of the CSS changes be animated (all). We typically stick with "all" since it's a good cover-all, but some conditions might change.
You can set multiple properties at one with comma seperation:
This is is the speed of the animation (in s or ms). I usually do in s (seconds) since it's more recognizable than milliseconds, but that's personal choice. If you put '.3s', it's going to animated between the two states in .3s. If you make it 5s, it will take 5 seconds to animate between the two states.
(Optional) This is the delay in which the animation starts. The default value is 0. It has to be a time value that comes second (the first time will always be the duration).
(optional) This is the complicated part. This is a setting to specify the "speed curve" of the animation. There are 5 basic options, and a more complex one, which the inspect element can help with:
This is a CSS function that basically is a 4 point curve to determine the speed of an animation. I can't exactly explain the curves, but you can use the inspect elements to adjust the curves (and the other options have variations that convert to these curves as well you should try).
The benefit to this set up is it's dependent on the interactions, and it smoothly interpolates between the two states. It's simple, you set up the transition, and you can set two states.
It doesn't just have to be hover, I use it a lot with javascript to add classes to elements and have the transition animate it (Like the logo in the header on the OA homepage)
Of course if you set up variables, you can use those as the values as well, this was you could have animations remove with a media query:
The cons of it are the animation itself can affect how the states interact (like the top of the page example, it rotates on hover, and can rotate out of the hover area, and immediately start to revert).
Keyframe animation requires you set up states of CSS for an element to go through, then using the animation property you apply it to an object.
To set up the animation, you need to use a keyframes rule.
You set up an animation by declaring an animation rule, a name for this animation, and the different steps it takes (or keyframes, as the name implies).
When setting up a keyframes rule, you need to set a name for it by putting it after the '@keyframes' and before the curly brace. It can be any text string you make up, you just have to use that same name when attaching it to an element.
The other part of the keyframes rule are the keyframe(s) themselves, or steps. This is declaring the CSS of an element during a particular part of an animation. You set them up like you would any CSS delcaration, but the selector is a percentage of the animation to a "to" or "from" keyword
To get an animation to work, you need at least one keyframe in the keyframes rule. If there is only one, the animation will go from its initial CSS to that keyframe, from the keyframe to its initial CSS, or from its initial state, to that keyframe, and back again:
You can use as many keyframes as you want to create some pretty complex animations. (The seasonal OA logos animate with this method See Here)
The animation CSS property is a shorthand propery for the following properties:
To get any animation from a keyframes rule to work on an element, you need to use the animation-name property and animation-duration property in CSS, either as the shorthand "animation" property, or the two expanded ones.
Pretty self explanitory, this attaches the animation set up in the keyframes rule to the element.
Of course, the example won't work, since it 0 seconds it's gone through the animation and stopped. You need the animation-duration property to tell the computer how long the animation should work for:
This property says how many times an animation is going to play before it stops. I've actually been using it on all the keyframe animations in this document so far so you can see the animations without them stopping.
The default value is 1, meaning it plays it once and stops, but you can set a int (number) to be the amount of times it repeats, or 'infinite' to make an animation play over and over
This is a property where you can make an element wait before animating (from when the element loads)
It's important to note that if you're setting up an animation shorthand property, you the delay has to be the second time measurement after the duration.
This is which direction the animation is played. By default it plays normal, but you can have it go backwards, forwards then backwards, or the reverse.
This is the same as the "timing function" in the transition property. You can set the speed curve of the animation:
This property determines if any of the states set in the animation remain before or after the animation.
By default, if a CSS animation on an item has not started, or after it finishes, and element has its default CSS applied to it. This property can have the first keyframe state before it starts, the last keyframe after it ends, or both.
As you've seen in most the above examples, all these properties can be combined into a shorthand combination, so long as the delay is after the duration:
The benefit to this set up is control. You can have multiple keyframes that an element animates between, rather than just from one state to the other.
You can also easity control the length and have elements repeat.
The cons are if you use it for interactive states (say having a :hover state have the animation there), the animation will abruptly cut off if it stops being hovered on.
Also, if you set an animation on an element further down that doesn't repeat infinitely, it could animate before its even seen.