Animations – Transition – Animation



Animations – Transition – Animation

0 0


animations-in-front-end

Slides for courses

On Github wontem / animations-in-front-end

Animations

Kirill Demura

CSS

  • transition
  • animation

JS

Transition

  Caniuse

property values default transition-property none | all | IDENT all transition-duration <time> 0s transition-timing-function <timingfunction> ease transition-delay <time> 0s

How?

.elem {
    transition: opacity 3s 1s;
}
.elem {
    transition-property: opacity;
    transition-duration: 3s;
    transition-delay: 1s;
}

.elem {
    transition: transform .3s, opacity 3s 1s;
}
.elem {
    transition-property: transform, opacity;
    transition-duration: .3s, 3s;
    transition-delay: initial, 1s;
}

Timing Functions

Cubic bezier curve

  Cubic bezier  Cubic bezier by Lea Verou  CSS Timing functions

Timing Functions

  Presets

Timing Functions

Steps

Timing Functions

Steps

Delay

Negative delay starts the animation at position corresponding absolute value of delay

Animation

  Caniuse property values default animation-name none | IDENT none animation-duration <time> 0s animation-timing-function <timingfunction> ease animation-delay <time> 0ms animation-iteration-count <number> | infinite 1 animation-direction normal | alternate | reverse | alternate-reverse normal animation-fill-mode none | forwards | backwards | both none animation-play-state running | paused running

How?

.elem {
    animation: some-madness 3s infinite;
}
.elem {
    animation-name: some-madness;
    animation-duration: 3s;
    animation-iteration-count: infinite;
}

.elem {
    animation: move .3s, hide 2s 1s;
}
.elem {
    animation-name: move, hide;
    animation-duration: .3s, 2s;
    animation-delay: initial, 1s;
}

@keyframes

@keyframes <name> rule defines scenario of animation

@keyframes some-madness {
    from {
        left: 0px;
        top: 200px;
    }
    40%, 60% {
        left: 400px;
        background: brown;
    }
    to {
        left: 200px;
        top: 0px;
    }
}

.elem {
    animation: some-madness 3s infinite;
}

Timing functions

For keyframed animations, the timing function applies between keyframes rather than over the entire animation

tbody:hover .elem {
  animation: move 2s ease 1; }

@keyframes move {
  0% {
    transform: translateX(-100%); }
  25% {
    transform: translateX(-75%); }
  50% {
    transform: translateX(-50%); }
  75% {
    transform: translateX(-25%); }
  100% {
    transform: translateX(0%); } }

Iteration count

Number of iterations. Infinite or float (for example 0.5 will play half of the animation cycle)

Fill mode

Specifies how a CSS animation should apply styles to its target before and after it is executing.

body:hover tr .elem {
	-webkit-animation: move 2s linear 1s;
			animation: move 2s linear 1s;
}

Fill mode example

See the Pen <a href='http://codepen.io/wontem/pen/MYGmWO/'>MYGmWO</a> by wontem (<a href='http://codepen.io/wontem'>@wontem</a>) on <a href='http://codepen.io'>CodePen</a>.

Direction

tbody:hover tr .elem {
	-webkit-animation: move 2s 2 ease-in
			animation: move 2s 2 ease-in
}

Play state

Debugging tools

  Paul Lewis - The Applied Science of Runtime Performance

Performance

  • Use translate() instead top/left positioning
  • Use translateZ(0) or translate3d(0, 0, 0) to force hardware acceleration

  will-change property  Accelerated Rendering in Chrome  On translate3d and layer creation hacks  High Performance Animations  Translate performanse

Example

See the Pen <a href='http://codepen.io/wontem/pen/PwwEPX/'><i class="fa fa-external-link"></i>&nbsp; Animation experiment</a> by wontem (<a href='http://codepen.io/wontem'><i class="fa fa-external-link"></i>&nbsp; @wontem</a>) on <a href='http://codepen.io'><i class="fa fa-external-link"></i>&nbsp; CodePen</a>.

Oops...

Explanation of strange behavior and some tricks

  Some Gotchas That Got Me

Transform and containing block

See the Pen <a href='http://codepen.io/wontem/pen/azdgwQ/'><i class="fa fa-external-link"></i>&nbsp; containing block</a> by wontem (<a href='http://codepen.io/wontem'><i class="fa fa-external-link"></i>&nbsp; @wontem</a>) on <a href='http://codepen.io'><i class="fa fa-external-link"></i>&nbsp; CodePen</a>.

Opacity stacking context

See the Pen <a href='http://codepen.io/wontem/pen/QwyXxq/'><i class="fa fa-external-link"></i>&nbsp; opacity stacking context</a> by wontem (<a href='http://codepen.io/wontem'><i class="fa fa-external-link"></i>&nbsp; @wontem</a>) on <a href='http://codepen.io'><i class="fa fa-external-link"></i>&nbsp; CodePen</a>.

!important vs animation

See the Pen <a href='http://codepen.io/wontem/pen/jEWoKW/'><i class="fa fa-external-link"></i>&nbsp; !important vs animation</a> by wontem (<a href='http://codepen.io/wontem'><i class="fa fa-external-link"></i>&nbsp; @wontem</a>) on <a href='http://codepen.io'><i class="fa fa-external-link"></i>&nbsp; CodePen</a>.

Moving an element along a circle

See the Pen <a href='http://codepen.io/wontem/pen/zxrVEm/'><i class="fa fa-external-link"></i>&nbsp; circular rotation</a> by wontem (<a href='http://codepen.io/wontem'><i class="fa fa-external-link"></i>&nbsp; @wontem</a>) on <a href='http://codepen.io'><i class="fa fa-external-link"></i>&nbsp; CodePen</a>.

Gradient animation

See the Pen <a href='http://codepen.io/wontem/pen/ZYWLYJ/'><i class="fa fa-external-link"></i>&nbsp; gradient animation</a> by wontem (<a href='http://codepen.io/wontem'><i class="fa fa-external-link"></i>&nbsp; @wontem</a>) on <a href='http://codepen.io'><i class="fa fa-external-link"></i>&nbsp; CodePen</a>.

  TransitionEvent

  • transitionend

  AnimationEvent

  • animationend
  • animationiteration
  • animationstart

Vendor prefix and camelCase may require. For example webkitAnimationEnd

Example

See the Pen <a href='http://codepen.io/wontem/pen/QwwKqy/'><i class="fa fa-external-link"></i>&nbsp; Toasts example</a> by wontem (<a href='http://codepen.io/wontem'><i class="fa fa-external-link"></i>&nbsp; @wontem</a>) on <a href='http://codepen.io'><i class="fa fa-external-link"></i>&nbsp; CodePen</a>.

Request animation frame

Runs callback function with before the next repaint.

var request;
function step(timestamp) {
    document.body.innerHTML = timestamp;
    request = requestAnimationFrame(step);
}
request = requestAnimationFrame(step);

setTimeout(function(){
    cancelAnimationFrame(request);
}, 1000);

  requestAnimationFrame for Smart Animating

Why?

  • 60fps
  • Don't call unwanted repaint unlike setInterval
  • Running the animation loop in a tab that’s not visible, the browser won’t keep it running

Web Animations API

See the Pen <a href='http://codepen.io/wontem/pen/emrrWg/'>emrrWg</a> by wontem (<a href='http://codepen.io/wontem'>@wontem</a>) on <a href='http://codepen.io'>CodePen</a>.

Moar useful links

  The state of animation 2014  SVG Animation and CSS Transforms

The end

  wontem