Animation with CSS Transitions Made Easy
There are currently two proposed additions to CSS to make animating easier and with better performance: CSS Transitions and CSS Animations. Both are unfortunately named. The later is complicated, overkill, and probably won’t be supported by non-Webkit browsers, which leaves CSS Transitions as becoming the de-facto way to natively animate on the Web. Besides reducing power-hungry Javascript-based animation, the declarative CSS Transitions opens the door for hardware acceleration and commercial quality games. iOS devices and Safari already do so for buttery smooth results, and hardware acceleration just might make it into Firefox 4.
How Do CSS Transitions Work?
Normally, when you change the value of a CSS property, it changes instantly. With CSS Transitions, they automagically animate over time. Imagine you want to have a rollover indication for links where, on hover, the link changes its background color and jigs up a bit. With CSS Transitions instead of having the effect happen instantly, it can smoothly animated. Here’s how you’d do it.
You’ll have to use the -moz and -webkit prefixes for these properties until the CSS transitions specification is finalized.
a{
transition-property: all;
transition-duration: 350ms;
transition-timing-function: ease-in-out;
position: relative;
background-color: white;
color: black;
}
a:hover{
top: -2px
background-color: black;
color: white;
}
You can even add an event listener to detect when the transition has finished:
aLink.addEventListener("transitionend", function(){
console.log("Transition Done!");
}, true);
Timing functions are defined by the control points for a cubic bezier curve of the form cubic-bezier(x1,y1,x2,y2). You can see some of the named timing functions and their values below. You can also define your own timing functions:

/* A more pronounced ease-in-out timing function. */ transition-timing-function: cubic-bezier(0.6, 0, 0.4, 1.0); /* A timing function which causes a little bounce. Note that values above 1 only work in Firefox. */ transition-timing-function: cubic-bezier(0, 0.35, .5, 1.3);
Using CSS Transitions with jQuery
To use CSS Transitions in practice, you’ll want some helper functions to handle setting and and unsetting the transition properties, handling completion callback, and stopping animations. Towards that end, I’ve written a quick jQuery plugin that allows you to do just that (as well as deals with an annoying Firefox bug.)
Using it is simple:
Some of the work that’s come before has assumed that you need to wait for the CSS transition properties to get applied before setting the properties to be animated. This is not the case.
// Makes all paragraph elements grow a border and then atrophy away.
$("p").animateWithCSS(
{border: "5px solid #555"}, // CSS properties to animate
1000, // Duration in ms
"cubic-bezier", // The timing function
function(){ // Callback
$("p").animateWithCSS({border: "0px"}, 350);
}
);
Get the CSS Transitions jQuery Plugin.
Demo
You’ll need a Webkit-based browser or a Firefox alpha to try this out.
Get the CSS Transitions jQuery Plugin.
Problems with CSS Transitions
Better timing functions. While using a cubic bezier curve is clever—it packs a lot of variation into just four numbers—it isn’t enough. While I was able to create a basic bounce animation, not only does the spec explicitly disallow it, but I can’t control even the basic parameters of the bounce (like how fast it returns). As John Resig mentioned in a blog post, it would be nice to supply a custom Javascript timing function (although this might break the ability to do hardware acceleration). A more CSS-like solution might be to allow authors to provide an arbitrary number of control points for a series of linked quadratic or cubic bezier curves (and to not restrict the y-axis points to the 0 to 1 range). That would enable fine-grained control of timing without requiring Javascript.
Ability to layer two animations on top of each other. If I’ve started an animation moving an element to the left, and half-way through want to start animating its opacity at a different rate, things get complicated very quickly. And by complicated, I mean impossible.
Ability to register an on step handler. We can currently register a “transitionend” event handler. I’d like to be able to do the same for “transitionstep”. This would make CSS Transitions potentially much more versatile—and allow frameworks to keep innovating on top of the transitions base instead of reverting to pure Javascript animation.
RT @aza Animation with CSS Transitions Made Easy | Follow @aza on Twitter | All blog posts
Hugh Isaacs II
You forgot Opera transitions, “-o-”.
ralpht
I find myself doing things like this:
elem = document.createElement(‘div’);
elem.style.opacity = 0;
document.appendChild(elem);
setTimeout(function() {
elem.style.transitionProperty=’opacity’;
elem.style.transitionDuration=’330ms’;
elem.style.opacity = 1;
}, 0);
when I want to do:
elem = document.createElement(‘div’);
elem.style.opacity = 0;
document.appendChild(elem);
elem.style.transitionProperty=’opacity’;
elem.style.transitionDuration=’330ms’;
elem.style.opacity = 1;
Is there a way to force changes made to style via JS to be committed for the purposes of animations without yielding?
Tobias
I wonder: How would a CSS-Based Game look like?
And thanks for the article!
Tomer Cohen
How do we make sure that people won’t overuse these animation techniques? I prefer not to see websites designed with powerpoint-style effects for each page load, and don’t wish to have a browser extension to block such eyecandy effects.
Geocities had enough of these stuff.
Robert O'Callahan
There is no chance transitions on CSS tranforms will be hardware accelerated for Firefox 4, sorry.
Transitions on opacity should be accelerated to a great extent, though.
Aza Raskin
If memory serves, CSS transitions (not transforms) will be hardware accelerated for at least Canvas elements in Firefox 4.
MarkC
I agree with you entirely on the need for better timing functions. I don’t think I’d want to go as far as a custom JS timing function, as that doesn’t seem significantly less complex than using JS to string together multiple separate transitions for the same effect. I would like more than just two bezier control points which are limited to the range 0 to 1:
http://www.peppertop.com/blog/?p=862
Without the ability to easily specify curves which bounce, overshoot, and perhaps are even discontinous, the web will still lag behind the sort of transitions that people are increasingly used to seeing on their mobile phones.
Jeffrey Gilbert
Demo doesn’t animate on Firefox 3.6.3 on PC
Jeffrey Gilbert
helps if i read the footnote. woops
Andrew Dupont
Also keep in mind that browsers try to “batch” CSS style property changes as much as possible, so multiple changes to a property within a single code block might not work as expected. I’m currently slugging it out with the WebKit people over this.
The nearly-in-beta script.aculo.us 2.0 will automatically use a CSS transition if it thinks it’s possible, and will even convert some style changes to their “-webkit-transform” equivalents so that they can be hardware-accelerated.
mike
isn’t that what you need for a bouncing ball animation?
(from the apple developer docs http://developer.apple.com/safari/library/documentation/InternetWeb/Conceptual/SafariVisualEffectsProgGuide/Animations/Animations.html#//apple_ref/doc/uid/TP40008032-CH6-SW1)
Listing 3 Using timing functions in keyframes
@-webkit-keyframes bounce {
from {
-webkit-transform: translateY(100px);
-webkit-animation-timing-function: ease-out;
}
25% {
-webkit-transform: translateY(50px);
-webkit-animation-timing-function: ease-in;
}
50% {
-webkit-transform: translateY(100px);
-webkit-animation-timing-function: ease-out;
}
75% {
-webkit-transform: translateY(75px);
-webkit-animation-timing-function: ease-in;
}
to {
-webkit-transform: translateY(100px);
}
}
lockoom
Nice. How about making it Opera (by adding -o- prefix) and future (no prefix) compatible?
Mansoor Ahmed
How can I do it, while displaying a slide show of the posts on my website? Like i want a delay of 5 seconds between each post to slide in, i want 5 – 7 posts on the slide show. And tell me how u added this demo in your post, like which tool u used?
Website Design Gloucestershire
It’s a real shame it’s not cross-browser compatible at the moment, but tested in Firefox and it’s a really nice effect!
I can’t wait for the day all browsers use the same rendering engine, it will make life so much easier!!
Regards
Rob
Dillon Kuehner
It’s only not cross-browser compatible because he chose not include all of the vendor prefixes and/or the general transition code without a prefix.
witek
What about Opera? They use “-o-transition” now for this.
eve isk
Hello. It is great that at least you pour more light on this issue. Thanks.
Jaanus
Another problem with transitions: you cannot layer and cascade them in CSS, and the syntax is just messy. See http://www.jaanuskase.com/en/2010/06/css3_transitions_and_their_lan.html
Christopher Giffard
Looks great – but I have a tiny nitpick:
There’s an error in your example – you’ve used the function name ‘animateWithCSS’ in your example code, but the real function name uses lower case s letters, as in ‘animateWithCss’.
It threw me for a while!
Thanks again – this is really interesting work.
Christopher Giffard
Another interesting observation – (forgive me if I’m repeating what we already know here!) in order to get opacity transitions hardware accelerated on the iPad and iPhone, you have to pair the opacity declaration with a transform. Both -webkit-transform: scale(1); and -webkit-transform: rotate(0deg); seemed to have the desired effect (but they do cause some subpixel misalignment issues around the edges of transformed elements, where content underneath them which should be obscured shows through.)
Rensa
Thanks, its awesome!
gucci belts
improve mens belts the laws that protect workers’ cheap gucci belts rights to combat poverty, according cheap louis vuitton belts for men cheap desiger belts to the Joseph Rowntree Foundation. gucci belts on sale He said the number louis vuitton belts cheap of people MBT often moving between unemployment and employment increased by 60% since 2006.
بنت الكويت
تااااااااااانكس
learn spanish free
Wow I really glad to read that your given article tips for Animation with CSS. I like your tips. CSS plugin is laso too god.Great post.
Loka
So nice ….!!
Its very helpfull to me…
Thanks U for the post
شات صوتي
University on her work; that lecture was similar to the one
شات كتابي
University on her work; that
Sheffield Website Design
Awesome post dude, Just what I was looking for. Following you on twitter now :) – Keep up the good posts.
@mgpwr
Bundyo
Yesterday I’ve tried to extend Zepto’s anim plugin to handle CSS transitions and transforms simultaneously and support all modern browsers. During doing so noticed that Opera doesn’t detect property changes if the transition is applied together with the CSS and transforms. A simple setTimeout after applying the transition first is doing wonders though.
wholesale beads
China wholesale beads store, free shipping, and very good post really
montanelas
vai levar no cu
montanelas
vai levar no cu!
Heker Shuk
Weird thing but the animation doesn`t work on my Chrome, is it my chrome or all of you guys?
KarlisG
This surealy has future!
css load animation generator
Check this one out – http://cssload.net – it’s the first loading css generator. I have already offered them to add the animation transitions from your web-site
Style Made Simple
My brother recommended I might like this web site. He was entirely right. This post truly made my day. You can’t imagine simply how much time I had spent for this information! Thanks!
Vijay Dev
Well written. Simple yet covers everything! Thanks for explaining the cubic bezier.
I hope by the time they are finished perfecting transitions, they will fix the problems you highlighted.
Web Site Design Gloucester
Thanks Aza, I have been looking for smoother css transitions using Jquery. Great article. Thanks
Stacey Marshburn
Pingback from koreyweldon.wordpress.com
Apple Nabs Microsoft’s Data Center Chief for…? [Blip] | koreyweldon
Buster Sydney
Once you discuss working with a person someone’s passion in addition to appreciation, they will possibly not ignore a student. Passion will clearly resume a student often. Many thanks.
pellet mill cooler
My brother recommended I might like this web site. He was entirely right. This post truly made my day. You can’t imagine simply how much time I had spent for this information! Thanks!
jack
allah
شات صوتي
thnks
goooooooooooood
min:)ااا
دردشة صوتية
thnks
goooooooooooood
min:)ااا
يلقفاعهخ
على مودك
ه0تا9ا9ا
référenceur seo
Je suis 100% d accord, j’apprécie votre style. Je dois dire que je ne regrette en rien de m’être abonné à votre weblog. Bonne journée
référencement internet http://agencewebmarketingmontpellier.wordpress.com référencement payant
Erika Rendon
Transitions are important. I like creating two classes and then using the animate class switch in the official jQuery UI
Here is a sample of some working code. I hope this helps someone.
demo
.newClass {
color: orange;
font-size: 10px;
}
.anotherNewClass {
color: pink;
font-size: 60px;
}
$(function() {
$( “#button” ).click(function(){
$( “.newClass” ).switchClass( “newClass”, “anotherNewClass”, 1000 );
$( “.anotherNewClass” ).switchClass( “anotherNewClass”, “newClass”, 1000 );
return false;
});
});
Etiam libero neque, luctus a, eleifend nec, semper at, lorem. Sed pede.
Run Effect
Etiam libero neque, luctus a, eleifend nec, semper at, lorem. Sed pede.
umair
I am new to CSS Transitions and no idea CSS Transitions so i have to conform something im making a runtime animation of a song
mean every song has its own type of animation its depends upon the songs.
in this web application in which im animating the song would be seen on video player as a video of the song diatonic accordion button would be shown in video which is pressed according to that song which i’ll got from the client in midi format.
so is it CSS Transitions is good for me is it capable of doing so
goutam naik
jkashflksdf;kshjsvnsksdhsnhfsf
rolas
hey, how i can make transition like in your random post above ?