CSS Transitions

Google Chrome
1.0
Apple Safari
3.2
Mozilla Firefox
4.0
Microsoft Internet Explorer
10
Opera
10.5








How to use transitions

If you haven't used transitions before, here's a brief introduction.
On the element you want to have animate, add the following CSS:
  1. #id_of_element {
  2. -webkit-transition: all 1s ease-in-out;
  3. -moz-transition: all 1s ease-in-out;
  4. -o-transition: all 1s ease-in-out;
  5. transition: all 1s ease-in-out;
  6. }
There is a lot of duplication due to vendor prefixes - until the specification if finalised, this will persist. If this bothers you, there are various tools such as CSS ScaffoldLESS, or my preference - SASS, that allow you to define mixins to avoid repetitive code.
Another approach is simply to write the CSS without the prefixes, then use Lea Verou's -prefix-free to add them in at runtime.
Something you definitely shouldn't do is to only include the webkit prefix. Tempting though it seems, particularly when developing for mobile devices, webkit isn't the only rendering engine!
It's worth noting as well that there isn't an -ms- prefix on these properties. IE10 was the first browser to ship without a prefix on these. The betas of IE10 did use the prefix however, so you may see code using -ms-. It's not needed though.
The syntax is pretty straightforward, you specify the property you want to animate, all or border-radius or color or whatever, the time to run, then the transition timing function. The options for the timing function are shown below.
Whenever any property changes, then it will animate instead of changing directly. This can be due to a different set of properties set on a pseudo class such as hover, or a new class or properties set by javascript. The example below uses :hover to change the properties – no javascript is needed.
To see the difference in speed, have a look at the speed test.

Different timing functions

Ease
Ease
In
Ease
Out
Ease
In Out
Linear
Custom
Awesome!
Hover on me
In addition to the built in timing functions, you can also specify your own. The excellent Ceaser CSS Easing Tool makes this very easy.
It's worth noting that the curves you produce can have negative values in them. The bezier curve for the last box above iscubic-bezier(1.000, -0.530, 0.405, 1.425), the negative values are causing it to 'take a run up', which looks pretty awesome!

Delays

The syntax for a CSS3 transition is of the form:
transition:  [ <transition-property> ||
               <transition-duration> ||
               <transition-timing-function> ||
               <transition-delay> ]
You will notice the final parameter is a delay - this let's you trigger things after an event has occurred. Below is a small demo showing this functionality.

Transition delays

Hover on me
This works by just adding a delay to each of the different circles. This is as easy as adding a transition-delay: 0.6s; to the element.

Advanced delays

You can set the way different properties animate differently. In this example the normal (blue) circle has this CSS (with the appropriate vendor prefixes):
  1. #dd_main2 {
  2. transition: all 1s ease-in-out;
  3. }
The 'Example 1' (green) circle has this CSS instead:
  1. #dd_main2 {
  2. transition-property: top, left;
  3. transition-duration: 1s, 1s;
  4. transition-delay: 0s, 1s;
  5. }
While the 'Example 2' (red) circle has this CSS instead:
  1. #dd_main2 {
  2. transition-property: top, left, border-radius, background-color;
  3. transition-duration: 2s, 1s, 0.5s, 0.5s;
  4. transition-delay: 0s, 0.5s, 1s, 1.5s;
  5. }
This allows us to animate the properties independently of each other, meaning that this simple technique can be used to create very complex animations.
Normal
Example 1
Example 2
Hover on me

Animatable properties

Regarding the properties you can animate, the best way is to experiment. The W3C maintain a list of properties that can be animated on the CSS Transitions spec. These include everything from background-color and letter-spacing to text-shadow and min-height. Many of these properties are not supported by default by jQuery animation, making CSS transitions much more useful out of the box. In addition, many browsers hardware accelerate animations that don't require repaints, namely opacity, 3D transforms and filters. To see the methods that Webkit accelerates, take a look at the AnimationBase.cpp code from the Webkit source. At the time of writing there are three classes defined here: PropertyWrapperAcceleratedOpacity,PropertyWrapperAcceleratedTransform and PropertyWrapperAcceleratedFilter. These are the animations that Webkit accelerates. Other browsers do things differently, but as Webkit is popular on mobile where these things matter most, it's worth noting this special case.
In reality, browsers are allowing more properties than these to be animated - box-shadow springs to mind as an obvious example. The table below is taken from the link above, and can be considered the minimum number of properties you would expect to be animatable.
Property NameType
background-colorcolor
background-imageonly gradients
background-positionpercentage, length
border-bottom-colorcolor
border-bottom-widthlength
border-colorcolor
border-left-colorcolor
border-left-widthlength
border-right-colorcolor
border-right-widthlength
border-spacinglength
border-top-colorcolor
border-top-widthlength
border-widthlength
bottomlength, percentage
colorcolor
croprectangle
font-sizelength, percentage
font-weightnumber
grid-*various
heightlength, percentage
leftlength, percentage
letter-spacinglength
line-heightnumber, length, percentage
margin-bottomlength
margin-leftlength
margin-rightlength
margin-toplength
max-heightlength, percentage
max-widthlength, percentage
min-heightlength, percentage
min-widthlength, percentage
opacitynumber
outline-colorcolor
outline-offsetinteger
outline-widthlength
padding-bottomlength
padding-leftlength
padding-rightlength
padding-toplength
rightlength, percentage
text-indentlength, percentage
text-shadowshadow
toplength, percentage
vertical-alignkeywords, length, percentage
visibilityvisibility
widthlength, percentage
word-spacinglength, percentage
z-indexinteger
zoomnumber

In addition to this, all browsers with transitions support animating CSS transforms, which proves to be invaluable.
To find out more about CSS3 transitions, read through the W3C specification.
Share on Google Plus

About Salman Ahmed Rajput

Hi I am Salman Ahmed, nicknamed as "Sam". I am a technology lover and also a Web Developer. My favorite programming language is PHP which means I usually develop web apps in PHP. Wanna Follow ? Yes? You can follow me at https://facebook.com/Sam.programer
https://twitter.com/SalmanRajz.
    Blogger Comment

0 comments:

Post a Comment

Don't Forget to Share and Comment