Back Out
The element steps back to a smaller size, as if moving into the distance, then glides off screen at that size. Pick the edge, or give several and a looping demo takes each in turn.
Draft discardedUntitled note
How to use it
- Paste the snippet just before
</head>. - Add
data-back-outto any element. - Choose an edge with
data-toif it shouldn’t leave by the bottom.
Options
Add any of these attributes to the same element.
data-to | The edge it leaves by: bottom, top, left or right. Give several and each loop uses the next. Default bottom. |
data-scale | Its size as it leaves. Default 0.7. |
data-distance | Travel this many px, fading out, instead of leaving the screen. |
data-duration | How long it takes to leave. Default 1100 ms. |
data-delay | How long it stays in view before it leaves. Default 1200 ms. |
data-loop | Fade back in, hold and back out again. |
data-hold | How long it stays between exits, when looping. Default 1400 ms. |
It leaves past the edge of the nearest container that clips its overflow (overflow: hidden or clip), or of the window. Put it in a clipping wrapper, or give the page overflow-x: clip, so it can’t make the page scroll sideways on its way out. It ends hidden (visibility: hidden) but still taking up its space. To play it when you choose, leave the attribute off and call animacious.backOut(el).
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/bundles/anime.umd.min.js"></script>
<script>
(function () {
// Back Out: the element steps back to a smaller size, as if moving into
// the distance, then glides off screen at that size.
// Usage: <div data-back-out>…</div>
// It plays once the element has been in view for data-delay ms. It leaves
// past the edge of the nearest container that clips its overflow
// (overflow: hidden or clip), or of the window, and ends hidden
// (visibility: hidden) but still taking up its space.
// Optional: data-to ("bottom", default, "top", "left" or "right"; give
// several, like "bottom left top", and each loop uses the next),
// data-scale (its size as it leaves, default 0.7), data-distance (px to
// travel, fading out, instead of leaving the screen),
// data-duration (ms, default 1100), data-delay (ms in view before it
// leaves, default 1200), data-loop (fade back in, hold and back out
// again), data-hold (ms to stay between exits when looping, default 1400).
// Script use: animacious.backOut(el, done) plays it on any element now,
// using its data-* options, and calls done afterwards.
var DEFAULTS = { to: 'bottom', scale: 0.7, duration: 1100, delay: 1200, hold: 1400 };
function reduced() {
return window.matchMedia('(prefers-reduced-motion: reduce)').matches;
}
// The box the element must leave to be "off screen".
function clipBox(el) {
for (var p = el.parentElement; p && p !== document.body; p = p.parentElement) {
var s = getComputedStyle(p);
if (s.overflowX !== 'visible' || s.overflowY !== 'visible') return p.getBoundingClientRect();
}
return { left: 0, top: 0, right: window.innerWidth, bottom: window.innerHeight };
}
function leave(el, to, done) {
var d = el.dataset;
var t = +d.duration || DEFAULTS.duration;
if (reduced()) {
anime.utils.set(el, { opacity: 0 });
if (done) done();
return;
}
var sc = +d.scale || DEFAULTS.scale;
anime.utils.set(el, { x: 0, y: 0, scale: 1 });
var dist = +d.distance;
var r = el.getBoundingClientRect();
var c = clipBox(el);
// Its shrunken box is smaller, so it needs less travel to clear the edge.
var gapX = r.width * (1 - sc) / 2;
var gapY = r.height * (1 - sc) / 2;
var o = { x: 0, y: 0 };
if (to === 'top') o.y = -(dist || r.bottom - gapY - c.top + 8);
else if (to === 'left') o.x = -(dist || r.right - gapX - c.left + 8);
else if (to === 'right') o.x = dist || c.right - r.left - gapX + 8;
else o.y = dist || c.bottom - r.top - gapY + 8;
anime.animate(el, {
// Step back first, a little dimmer, then go.
scale: { to: sc, duration: t * 0.35, ease: 'inOut(2)' },
x: { to: o.x, delay: t * 0.35, duration: t * 0.65, ease: 'in(3)' },
y: { to: o.y, delay: t * 0.35, duration: t * 0.65, ease: 'in(3)' },
opacity: dist
? [{ to: 0.75, duration: t * 0.35, ease: 'out(2)' }, { to: 0, duration: t * 0.65, ease: 'in(2)' }]
: { to: 0.75, duration: t * 0.35, ease: 'out(2)' },
onComplete: function () {
if (done) done();
}
});
}
function cycle(el) {
var d = el.dataset;
var s = el._bko;
var list = (d.to || DEFAULTS.to).split(/[\s,]+/);
leave(el, list[s.n++ % list.length], function () {
if (d.loop === undefined || d.loop === 'false') {
el.style.visibility = 'hidden';
return;
}
// Come back quietly in place, then go again once it's on screen.
setTimeout(function () {
anime.utils.set(el, { x: 0, y: 0, scale: 1, opacity: 0 });
anime.animate(el, {
opacity: 1,
duration: 400,
ease: 'out(2)',
onComplete: function () {
setTimeout(function () {
if (s.visible) cycle(el);
else s.waiting = true;
}, +d.hold || DEFAULTS.hold);
}
});
}, 400);
});
}
window.animacious = window.animacious || {};
window.animacious.backOut = function (el, done) {
leave(el, (el.dataset.to || DEFAULTS.to).split(/[\s,]+/)[0], function () {
el.style.visibility = 'hidden';
if (done) done(el);
});
};
function init() {
var io = new IntersectionObserver(function (entries) {
entries.forEach(function (e) {
var s = e.target._bko;
s.visible = e.isIntersecting;
if (!e.isIntersecting) return;
if (!s.started) {
s.started = true;
var wait = e.target.dataset.delay;
setTimeout(function () { cycle(e.target); }, wait === undefined ? DEFAULTS.delay : +wait || 0);
} else if (s.waiting) {
s.waiting = false;
cycle(e.target);
}
});
}, { threshold: 0.2 });
document.querySelectorAll('[data-back-out]').forEach(function (el) {
if (el._bko) return;
el._bko = { n: 0 };
if (!reduced()) io.observe(el);
});
}
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', init);
else init();
})();
</script><div class="card" data-back-out>Draft discarded</div>
<!-- or leaving by the left: -->
<div class="card" data-back-out data-to="left">Draft discarded</div>