less janky compared to JS animation
browser gets to decide how to optimize
CSS animations are easy to create
CSS Animations Advantages
@keyframes slidein {
from {
margin-left: 10%;
}
to {
margin-left: 90%;
}
}
@keyframes slidein {
0% {
margin-left: 10%;
}
100% {
margin-left: 90%;
}
}
@keyframes slidein {
0% {
margin-left: 0%;
}
100% {
margin-left: 50%;
}
}
.sliding {
animation: slidein 2s;
}
element.animate([
// keyframes
{ transform: 'translateY(0px)' },
{ transform: 'translateY(-300px)' }
], {
// timing options
duration: 1000,
iterations: Infinity
});
const animation = element.animate([
// keyframes
], {
// timing options
});
// Animation methods
animation.pause()
animation.play()
// Event handlers
animation.onfinish = () => handleFinish()
import { Animation } from 'react-web-animation'
class Ball extends Component {
bounceHeight = t => {
// same as previous example
}
getKeyFrames = () => {
// same as previous example
}
render() {
return (
<Animation
keyframes={this.getKeyFrames()}
timing={this.getTiming(1400)}>
<div id="ball"/>
</Animation>
);
}
The transform and opacity animations do not affect normal flow or DOM environment.
GPU will handle animate movement, scaling, rotation, opacity and affine transforms
Offloading to the GPU
avoid animating color, margin, padding, top, bottom, etc...
CSS Animations
simple animations
complex animations
Web Animations API