CSS Transitions-Missing Chapter
CSS Transitions-Missing Chapter
In CSS, when you change the value of a property, the change is sudden. For
example, let's say that you have the following CSS where on hover,
the transform property's translate3d function has a different value:
#box img {
#box img:hover {
cursor: pointer;
Transitions slow down the sudden change in properties. They allow you to
specify how long a particular property change will take place. They allow you to
specify what kind of easing will be applied in going from the current property
value to another. Transitions basically allow you to animate the property
value changes.
With a transition applied, hover over the same logo in the following example:
This time around, instead of seeing a sudden change in the image, you can
actually visualize the intermediate positions as well. You see a gradual change
as the image slides up. You see an animation. If you interrupt the slide by
hovering away before the logo reaches its final position, notice that the
transition simply animates back to its original state without any fuss. Isn't that
pretty awesome?
In the next section, let's continue this excitement and add a transition of our
own!
Adding a Transition
Now that you have a pretty good idea of what a transition is and what it is
capable of, let's go ahead and use one. To follow along, create a new HTML
document and copy/paste the following HTML into it:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
background-color: #FFF;
margin: 30px;
margin-top: 10px;
#box {
width: 350px;
height: 350px;
overflow: hidden;
background-color: #F2F2F2;
#box img {
transform: translate3d(0, -350px, 0);
#box img:hover {
cursor: pointer;
</style>
</head>
<body>
<div id="box">
<script src="//www.kirupa.com/prefixfree.min.js"></script>
</body>
</html>
If you preview this document, you will see a picture of our HTML5 logo over a
yellow background. When you hover over this image, the picture will suddenly
change to show the same logo over the black background. If this sounds
familiar, it should be. This is identical to the example without the transition
you saw a few paragraphs earlier!
What we are going to do is add a CSS transition to make the change in the
image's position look smooth. Take a look at your #box img style rule:
#box img {
Just above the transform property declaration, add the following highlighted
line that contains our transition declaration:
#box img {
Once you've added the highlighted line, preview this document in your browser
again. Hover over the image, and (if everything worked properly) you will now
see your image gently slide from one position to another. That's all there is to
it. In the next few sections, let's look in detail at the line you added and learn
more about transitions than you ever thought you needed to know.
Now that you have a working example that uses a transition, let's try to
understand why it works. To reuse a graphic that I used in my Introduction to
Animation in HTML tutorial, a transition basically works by interpolating the
points between a start state and an end state:
The start state is defined by whatever value a CSS property you
are wishing to transition has initially:
#box img {
The end state is the final value of that same CSS property:
#box img:hover {
cursor: pointer;
Normally, this jump from the start state to the end state is
sudden as you saw a few times already. With a CSS transition
in place, though, that jump is gradual and defined entirely by
your duration and whatever easing / timing function your
transition specified:
transition: transform .5s ease-in;
Transition Property
One thing to note is that you can't just specify any CSS property for
your transition-property. The property has to animatable. That might sound
like a major buzzkill, but it really isn't. For the most part, almost any CSS
property you want to use as part of a transition is probably animatable already.
You can view the full list of animatable properties here and some additional
ones here (note the value of the column marked Anim.)
Transition Duration
ease
linear
ease-in
ease-out
ease-in-out
step-start
step-end
steps()
cubic-bezier()
You are not done just yet. There is actually a lesser used fourth
value that you can set on your transition declaration, and this
value maps to the transition-delay property:
transition-property: all;
transition-duration: .5s;
transition-timing-function: ease-in;
transition-delay: .1s;
obj.style.transitionDuration = ".2s";
If you let this code execute, what do you think our transition
will look like. Logically, since you only changed the transition's
duration to .2 seconds, you would expect the transition to
virtually look as follows:
The end result is a string that looks exactly like our initial
transition declaration defined in the CSS. To me, this looks
awkward. The way I work around this is by having all of the
transition properties declared separately as longhand variants:
transition-property: width;
transition-duration: 1s;
transition-timing-function: ease-out;
Multiple Transitions
transition-duration: .5s;
transition-timing-function: ease-out;
The last thing (for real this time) we are going to mention is
the transitionEnd event. When a transition has run to
completion, the elements affected by the transition fire
the transitionEnd event. There are a lot of cool things you can
do by reacting to this event, but I won't be explaining it here.
Instead, all of those cool things are covered in great detail in
the transitionEnd Event tutorial there.
Wrapping It All Up