Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS3 transitions chaining

What is a syntactically clean solution to run a chain of individual CSS3 transitions on a single element, one by one? An example:

  • set left to 10px and opacity to 1 through 200ms
  • set left to 30px through 500ms
  • set left to 50px and opacity to 0 through 200ms

Can this be done without JavaScript? If not, how to code it cleanly with JavaScript?

like image 220
Kos Avatar asked Sep 03 '11 16:09

Kos


People also ask

Can you have multiple transitions CSS?

You can transition two (or more) CSS properties by separating them with a comma in your transition or transition-property property. You can do the same with duration, timing-functions and delays as well. If the values are the same, you only need to specify one of them.

Can you use keyframes with transition?

A keyframe can be a “step” If we set up a keyframe animation to change the background color of an element to change from orange to black (because orange is the new black, after all) on hover over five seconds, it will do exactly that. It will divide that change up over time and make the transition.

What is css3 transition?

CSS transitions provide a way to control animation speed when changing CSS properties. Instead of having property changes take effect immediately, you can cause the changes in a property to take place over a period of time.


2 Answers

I believe you want a CSS3 animation where you define the CSS styles at different points in the animation and the browser does the tweening for you. Here's one description of it: http://css3.bradshawenterprises.com/animations/.

You will have to check on browser support for your targeted browsers.

Here's a demo that works in Chrome. The animation is pure CSS3, I only use Javascript to initiate and reset the animation:

http://jsfiddle.net/jfriend00/fhemr/

The CSS could be modified to make it work in Firefox 5+ also.

#box {
    height: 100px; 
    width: 100px; 
    background-color: #777;
    position: absolute;
    left: 5px;
    top: 5px;
    opacity: 0;
}

@-webkit-keyframes demo {
    0% {
        left: 10px;
    }
    22% {
        opacity: 1;
    }
    77% {
        left: 30px;
    }
    100% {
        left: 50px;
        opacity: 0;
    }
}

.demo {
    -webkit-animation-name: demo;
    -webkit-animation-duration: 900ms;
    -webkit-animation-iteration-count: 1;
    -webkit-animation-timing-function: ease-in-out;
}    
like image 54
jfriend00 Avatar answered Sep 17 '22 05:09

jfriend00


In pure CSS this can be done with the transition-delay property, with it you can delay the second and third transition.

I personally would like a JS solution better. timeouts or the "transitioned" event can be used to achieve this.

I would also suggest the script.aculo.us (or the beta v2: scripty2), it is especially designed to make programming these kinds of things efficient and easy.

like image 23
dtech Avatar answered Sep 17 '22 05:09

dtech