Animate
Animate CSS values with JavaScript.
- The built-in easings are
linear,elastic,quad,quint,circ,backandbounce. You can also pass your own function. - Animate a relative value with a string:
'+25'or'-25'adds to or takes from the value the property has when the animation starts. to()animates CSS values.delay()holds back the animations that come after it. Both return the animation, so calls chain.- One
Animateis for one element. To animate several, make one for each. - It uses
requestAnimationFrame. With CSS transitions most of this is obsolete, so use them when you can.
<div class="animate-header"> <div class="animate-actions"> <button type="button" id="animate-width">width 100px</button> <button type="button" id="animate-height">height 100px</button> <button type="button" id="animate-fade-out">fade out</button> <button type="button" id="animate-fade-in">fade in</button> <button type="button" id="animate-plus25">add 25 to width</button> <button type="button" id="animate-sequence">sequence</button> </div>
<div class="animate-easing"> <label for="animate-easing">easing</label> <select id="animate-easing" class="system"> <option selected value="linear">linear</option> <option value="elastic">elastic</option> <option value="quad">quad</option> <option value="bounce">bounce</option> <option value="back">back</option> <option value="quint">quint</option> <option value="circ">circ</option> </select> </div></div>
<div class="animate-playground"> <div class="animate-box" id="animate-box"></div></div>var box = document.getElementById('animate-box');var easing = document.getElementById('animate-easing');var animation = new ML.Animate(box);
// The easing is read when you click and passed as a setting of that one animation. Settings// given to the constructor, as in `new ML.Animate(box, { duration: 500 })`, are the defaults// for every animation, and the settings of to() override them.function settings() { return { easing: easing.value };}
function on(id, handler) { document.getElementById(id).addEventListener('click', handler);}
on('animate-width', function () { animation.to({ width: 100 }, settings());});
on('animate-height', function () { animation.to({ height: 100 }, settings());});
on('animate-fade-out', function () { animation.to({ opacity: 0 }, settings());});
on('animate-fade-in', function () { animation.to({ opacity: 1 }, settings());});
// A string that starts with + or - is relative: it is added to the current value.on('animate-plus25', function () { animation.to({ width: '+25' }, settings());});
// to() and delay() return the animation, so they chain. Each delay is counted from the moment// you call it, not from the end of the one before: 2000 here is one second after the 1000.on('animate-sequence', function () { animation .to({ width: '+25', left: '+10' }, settings()) .delay(1000) .to({ opacity: 0 }, settings()) .delay(2000) .to({ opacity: 1, height: '+50' }, settings());});Settings
Section titled “Settings”new ML.Animate(element, settings) takes the settings every animation of that element starts from.
to(props, settings, callback) can override them for that one animation.
| Setting | Default | What it is |
|---|---|---|
duration |
200 |
the length of the animation, in ms |
easing |
'linear' |
the name of a built-in easing, or your own function from progress to value |
A function is given the progress of the animation, from 0 to 1, and returns how far along the value should be, also from 0 to 1. It should return 0 for 0 and 1 for 1.
var animation = new ML.Animate(el, { duration: 500, easing: 'bounce' });
animation.to({ width: 100 }, {}, function () { console.log('animation complete');});
animation.to({ opacity: 0 }, { duration: 1000, easing: 'quad' });- Delays are counted from the moment you call them, not from the end of the one before. For a second delay of
one second after a first of one second, pass
2000(see the sequence in the demo). - Each
delay()lets exactly one waitingto()through. In a chain that is what you want. Twoto()calls after onedelay()leave the second waiting until anotherdelay()comes. - Only px values, and
opacity, can be animated. A number is the target, and a trailingpxis accepted. Anything that is not a number, such asauto, is left alone. - Property names can be CSS names with hyphens (
'margin-left') or in camelCase (marginLeft). - A negative number counts as relative:
-10means “10 less than now”, not “to -10”. - Animations on the same instance can overlap, and each has its own callback.
- A target given as a number or a plain string is eased from the value the property has on each frame, so the curve is steeper at the start than its name suggests. A relative target follows the curve exactly.
- An animation cannot be cancelled once it has started.
See the generated Animate class reference for every method and setting.
Enhancements
Section titled “Enhancements”A checklist. An unticked item is not built, and not promised: it says what is missing today. A ticked item has been built.
- A real sequence. Every
delay()is counted from the moment you call it, and lets exactly one waitingto()through, so a delay at the end of a chain does nothing. A sequence would start each step when the one before it has ended. - Stopping. An animation, or the queue of delays, cannot be cancelled.
- More properties. Only
pxvalues andopacitywork. Unitless properties (z-index) and other units (%,em) cannot be animated. - camelCase names.
marginLeftwas ignored, and the name had to bemargin-left. Both work now. - Negative targets.
-10means “10 less than now”, so “to -10” cannot be written. - Easing the whole move. A target that is not relative is eased from the value read on every frame, so the easing is applied to the distance that is left. A relative target follows the curve exactly.
- Reduced motion.
prefers-reduced-motionis not honored. - More easings. Named ones such as
ease-in-out.