Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Animations and Transitions

Animations and Transitions

CSS Day, 14th June 2013

Peter Gasston

June 14, 2013
Tweet

More Decks by Peter Gasston

Other Decks in Technology

Transcript

  1. 3 1. The past 2. The present 2.1. Transitions 2.2.

    Animations 2.3. Performance 2.4. Soft skills 3. The future
  2. DHTML! $(foo).animate({ height: '50%', opacity: 0.25, }); function doMove() {

    foo.style.left = (foo.style.left+10)+'px'; setTimeout(doMove,20); }
  3. Hooray! Except that implementation is incomplete, not in accordance with

    the spec, and has many bugs. But otherwise, yes: Hooray!
  4. -webkit- prefixed in Safari 6 Unprefixed in Safari 7 and

    all other modern browsers Transitions
  5. Multiple Transitions .foo { transition-property: gorilla, color; transition-duration: 1s, 5s;

    } /* Firefox */ .foo { transition: color 5s; } /* WebKit / Blink */ .foo { transition: all 5s; }
  6. Animations @keyframes chimp { 0% { background-color: yellow; } 40%

    { background-color: green; } 100% { background-color: cyan; } } 0% == from 100% == to
  7. Animations @keyframes chimp { from, to { width: 100px; }

    40% { background-color: green; width: 200px; } to { background-color: cyan; } }
  8. The Spec Says… “If a property is not specified for

    a keyframe, or is specified but invalid, the animation of that property proceeds as if that keyframe did not exist.”
  9. Animations @keyframes chimp { from { border-width: 10px; width: 100px;

    } 40% { width: 20px; } to { border-width: 1px; width: 1px; } }
  10. Animations @keyframes chimp { from { width: 100px; animation-timing-function: ease-in;

    } 40% { width: 20px; animation-timing-function: ease-out; } to { width: 1px; } }
  11. Animations If animation-direction has a value of reverse or alternate-reverse,

    the value of animation-timing-function is also reversed. e.g. ease-in becomes ease-out.
  12. Fill Mode backwards will apply the properties in the from

    keyframe before the animation begins. forwards will apply the properties in the to keyframe when the animation ends. both will apply the properties in the from keyframe before the animation begins, and the properties in the to keyframe when the animation ends.
  13. Fill Mode div { animation: foo 1s 1s; } animation-fill-mode:

    backwards; animation-fill-mode: forwards; animation-fill-mode: both;
  14. Multiple Animations @keyframes one { to { background-color: red; }

    } @keyframes two { to { background-color: blue; width: 200px; } } div { animation: two 1s, one 1s; }
  15. Multiple Animations div { animation: foo 1s infinite, bar 2s

    1s alternate-reverse, baz 500ms alternate infinite; } Good luck with that!
  16. Transitionable The properties that can be transitioned are generally those

    which accept quantifiable values: length, color, number, or percentage http://thewebevolved.com/support/animation/properties/
  17. Transitionable The auto value isn’t quantifiable, so can’t be animated

    to or from. Values inside the calc() function are, so can be.
  18. Transitionable The visibility property is a special case: it toggles

    between hidden and visible at the end* of the animation, with no transition. *or the beginning, depending on your parameters
  19. Transitionable Some properties will be implicitly transitioned - e.g. a

    transition on the font-size property makes other properties that have values relative to it - line-height, margin, etc - transition also. The exception is IE10, where only line-height will be transitioned.
  20. Transitionable http://thewebevolved.com/support/animation/properties/ div { background: url('foo.png') no-repeat 25% 25%, url('bar.png')

    no-repeat 50% 50%, url('baz.png') no-repeat 75% 75%; transition: 2s; } div:hover { background-position: 100% 25%, 0 50%, 50% 75%; }
  21. Spec Violation “Gradient[s]… must have the same type (radial or

    linear) and same number of stops in order to be animated.” No browser I’ve tested complies with this.
  22. TIL When shorthand properties are transitioned, the transitionend event fires

    for all of the individual properties. Except in Chrome and Safari, if you explicitly state the shorthand property as the value for transition-property.
  23. TIL Firefox doesn’t fire the transitionend event when pseudo elements

    are transitioned, and the event object doesn’t contain the pseudoElement attribute.
  24. TIL When you use the all value for transition-property, the

    implicit transitions that fire the transitionend event vary between browsers.
  25. TIL In Firefox, the animationstart event doesn’t fire if the

    animation begins before the DOMContentLoaded event.
  26. Spec Violation “When the animation-duration is 0s, animation-fill-mode still applies…

    also, animation events are still fired.” No browser I’ve tested does this.
  27. Performance “Use CSS keyframe animation or CSS transitions, if at

    all possible. The browser can optimize painting and compositing bigtime here.” http://paulirish.com/2012/why-moving-elements-with-translate-is-better-than-posabs-topleft/
  28. Performance Chrome uses graphics acceleration automatically for transitioned elements, but

    in Safari, especially for iOS, you may have to force it: div { -webkit-transform: rotateY(0deg); } However… http://blog.tumult.com/2013/02/28/transform-translate-vs-top-left/
  29. Performance … semi-opaque or transparent elements may see little tangible

    benefit from the rotateY hack - and it could even reduce performance. http://blog.tumult.com/2013/02/28/transform-translate-vs-top-left/
  30. Time “Animation can help to provide context. It helps brains

    understand how the information flows.” https://medium.com/design-ux/926eb80d64e3
  31. Speed Speed it all up. People aren’t on your site

    to admire your animation skills they’re there to achieve an aim.
  32. 12 Basic Principles 1. Squash and stretch 2. Exaggeration 3.

    Staging 4. Anticipation 5. Motivation 6. Secondary action 7. Overlap 8. Follow-through 9. Balance 10. Timing 11. Rhythm 12. Camera movement http://www.digitalartsonline.co.uk/features/illustration/12-rules-of-animation/
  33. The Future var anim = el.animate({}, t); var anim =

    el.animate({color:'red'}, 2); https://dvcs.w3.org/hg/FXTF/raw-file/default/web-anim/index.html
  34. The Future var anim = new Animation(el, {color:'red'}, 2); document.timeline.play(anim);

    var anim = new Animation( el, {color:'red'}, { iterationDuration: 3, playbackRate: 2 } );