CSS: Animation vs. Transition

你离开我真会死。 提交于 2019-11-27 16:45:36

It looks like you've got a handle on how to do them, just not when to do them.

A transition is an animation, just one that is performed between two distinct states - i.e. a start state and an end state. Like a drawer menu, the start state could be open and the end state could be closed, or vice versa.

If you want to perform something that does not specifically involve a start state and an end state, or you need more fine grain control over the keyframes in a transition, then you've got to use an animation.

I'll let the definitions speak for themselves (according to Merriam-Webster):

Transition: A movement, development, or evolution from one form, stage, or style to another

Animation: Endowed with life or the qualities of life; full of movement

The names appropriately fit their purposes in CSS

So, the example you gave should use transitions because it is only a change from one state to another

paulcpederson
  1. Animation takes a lot more code unless you're using the same transition over and over, in which case an animation would be better.

  2. You can have different effects for sliding in and out without an animation. Just have a different transition on both the original rule and the modified rule:

    .two-transitions {
        transition: all 50ms linear;
    }
    
    .two-transitions:hover {
        transition: all 800ms ease-out;
    }
    
  3. Animations are just abstractions of transitions, so if the transition is hardware accelerated, the animation will be. It makes no difference.

  4. Both are very modern.

  5. My rule of thumb is if I use the same transition three times, it should probably be an animation. This is easier to maintain and alter in the future. But if you are only using it once, it is more typing to make the animation and maybe not worth it.

Grecdev

A shorter answer, straight on point:

Transition:

  1. Needs a triggering element (:hover, :focus etc.)
  2. Only 2 animation states (start and end)
  3. Used for simpler animations (buttons, dropdown menus and so on)
  4. Easier to create but not so many animation/effect possibilities

Animation @keyframes:

  1. It can be used for endless animations
  2. Can set more than 2 states
  3. No boundaries

Both use CPU acceleration for a much smoother effect.

Animations are just that - a smooth behavior of set of properties. In other words it specifies what should happen to a set of element's properties. You define an animation and describe how this set of properties should behave during the animation process.

Transitions on the other side specify how a property (or properties) should perform their change. Each change. Setting a new value for certain property, be it with JavaScript or CSS, is always a transition, but by default it is not smooth. By setting transition in the css style you define different (smooth) way to perform these changes.

It can be said that transitions define a default animation that should be performed every time the specified property has changed.

Is there something that can be said about performance. Do both take advantage of h/w acceleration?

In modern browsers, h/w acceleration occurs for the properties filter, opacity and transform. This is for both CSS Animations and CSS Transitions.

I believe CSS3 animation vs CSS3 transition will give you the answer you want.

Basically below are some takeaways :

  1. If performance is a concern, then choose CSS3 transition.
  2. If state is to be maintained after each transition, then choose CSS3 transition.
  3. If the animation needs to be repeated, choose CSS3 animation. Because it supports animation-iteration-count.
  4. If a complicated animation is desired. Then CSS3 animation is preferred.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!