CSS3 Transition: Different transition for *IN* and *OUT* (or returning from transitioned state)

HandiworkNYC.com picture HandiworkNYC.com · Jan 20, 2012 · Viewed 37.3k times · Source

Original Question... updated working code below:

Answer

bookcasey picture bookcasey · Jan 20, 2012

Here is a simplified test case:

div {
    background: blue;
    opacity: 0;
    transition: opacity 2s ease-in-out;
}

div.loading {
    opacity: 1;
    background: red;
    transition: opacity 2s ease-in-out, background 1s ease-in;
}

Notice how the opacity fades the same in and out, but the background only fades in, and immediately turns blue on fade out.

I used :hover as an example, but it should work the same when adding and removing classes with JavaScript.

Demo

If you'd like a more specific example please provide a reduced test case on dabblet or Jsfiddle.