Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use cases for CSS transitions and CSS animations

As far as I understand, there is no such thing we can implement using css transitions, but we can not to implement using css animations, but not vice versa.

That is, any transition has a css animation equivalent. For example, this one

.ablock:hover {                                                   
  position: relative;                                             
  -moz-transition-property: background-color, color;              
  -moz-transition-duration: 1s;                                   
  -webkit-transition-property: background-color, color;           
  -webkit-transition-duration: 1s;                                                             
  color: red;                                                     
  background-color:pink;                                          
}

is an equivalent of following:

.ablock:hover {
    -moz-animation-duration:1s;                                   
    -moz-animation-name:transition;                               
    -webkit-animation-duration:1s;                                
    -webkit-animation-name:transition;                            
}       

@-moz-keyframes transition {                                      
    to {                                                          
        color: red;                                               
        background-color: pink;                                   
    }
}       

@-webkit-keyframes transition {                                   
    to {                                                          
        color: red;                                               
        background-color: pink;                                   
    }
}

My question is - if we a talking about browser supporting both css transitions and animations, what are use cases for choosing one or another approach? As for transitions, I can name only one - they have more succinct syntax, we don't have to copy paste huge chucks of code for @-moz-keyframes, @-webkit-keyframes and so on.

As for control from javascript, flexibility and complexity animations are much more appropriate tool (at least, at first glance). So, what are use cases?

UPD: OK, let me try to list interesting info found in questions.

  • This one is contributed by Roman Komarov. Say, we have a div and child div. While parent div is hovered, we are transitioning the child element. Once we are taking away the mouse, transition is cancelled. Duration of this cancellation is exactly the time we've already spend for transitioning. Animation is cancelled "immediately". I don't know, nevertheless, how standard are those two behaviours.
like image 826
shabunc Avatar asked Aug 09 '11 07:08

shabunc


People also ask

What is the importance of using CSS transition and CSS animation?

CSS transitions offer you a way to create simple animations that always start as the result of triggering a CSS property change. Transitions can animate only between a start and end state, and each state is controlled by existing CSS property values.

What is the use of CSS animations?

CSS animations make it possible to animate transitions from one CSS style configuration to another. Animations consist of two components, a style describing the CSS animation and a set of keyframes that indicate the start and end states of the animation's style, as well as possible intermediate waypoints.

Should I use transition or animation CSS?

CSS transitions are generally best for simple from-to movements, while CSS animations are for more complex series of movements. It's easy to confuse CSS transitions and animations because they let you do similar things.

What is the difference between animation and transition in CSS?

Transitions animate a object from one point to another. Animation allows you to define Keyframes which varies from one state to another with various properties and time frame. Use transition for manipulating the value using JavaScript. Flexibility is provided by having multiple keyframes and easy loop.


2 Answers

  • Animations can be looped (and there can be keyframes, yeeah).
  • Transitions can be more flexible and you can easily make transitions to different values and in different circumstances.

While you can emulate some transitions by animations (like you mentioned in your post), the transitions are just more powerful:

  • You just tell which properties you must animate and in which conditions (using the different selectors)
  • You can trigger the transition in different ways:
    1. Changing properties in CSS for pseudo-classed :hover, :active etc. (Creating pure CSS UI)
    2. Changing properties in different classes for different purposes.
    3. Changing properties in inline styles: in conjunction with JS it's just more powerful than animations.
like image 131
kizu Avatar answered Oct 14 '22 08:10

kizu


With transitions you are able to transition between any value of the defined property, which you want to be transitioned. As an example, you want to transition the color of a link, when it's hovered and active:

a {
    color: #000;
    transition: color .4s ease;
}
a:hover {
    color: #888;
}
a:active {
    color: #faa;
}

You are independent, which color you choose. Now if you want to use the animation style, you have to explicitly set the color value for the animation states. And you are not able to easily animate between the three states: normal, hover and active. You need more complex definitions. I'll try this one with animations:

a {
    color: #000;
    animation-duration: 0.4s;
    animation-fill-mode: forwards;
    animation-name: toDefault;
}
a:hover {
    animation-duration: 0.4s;
    animation-fill-mode: forwards;
    animation-name: toHover;
}
a:active {
    animation-duration: 0.4s;
    animation-fill-mode: forwards;
    animation-name: toActive;
}
@keyframes toDefault {
    to {
        color: #000;
    }
}
@keyframes toHover {
    to {
        color: #888;
    }
}
@keyframes toActive {
    to {
        color: #faa;
    }
}

Now this does not include the animation back to the state before. I'm not sure if you can even fetch that.

So in short: with transitions you are able to animate an undefined set of properties and values, whilst keyframe animations are used for well defined animations and/or transitions.

like image 34
Regaddi Avatar answered Oct 14 '22 08:10

Regaddi