Slide Out
The element picks up speed and slides off screen towards any edge. Pick the edge, or give several and a looping demo takes each in turn.
Conversation archivedMoved out of your inbox
How to use it
- Paste the snippet just before
</head>. - Add
data-slide-outto any element. - Choose an edge with
data-toif it shouldn’t leave to the right.
Options
Add any of these attributes to the same element.
data-to | The edge it leaves by: right, left, top or bottom. Give several, like right bottom left, and each loop uses the next. Default right. |
data-distance | Travel this many px, fading out, instead of leaving the screen. |
data-duration | How long it takes to leave. Default 700 ms. |
data-delay | How long it stays in view before it leaves. Default 1200 ms. |
data-loop | Fade back in, hold and slide 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, such as when something is dismissed, leave the attribute off and call animacious.slideOut(el).
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/bundles/anime.umd.min.js"></script>
<script>
(function () {
// Slide Out: the element speeds up and slides off screen towards any edge.
// Usage: <div data-slide-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 ("right", default, "left", "top" or "bottom"; give
// several, like "right bottom left", and each loop uses the next),
// data-distance (px to travel, fading out, instead of leaving the screen),
// data-duration (ms, default 700), data-delay (ms in view before it
// leaves, default 1200), data-loop (fade back in, hold and slide out
// again), data-hold (ms to stay between exits when looping, default 1400).
// Script use: animacious.slideOut(el, done) slides any element out now,
// using its data-* options, and calls done afterwards.
var DEFAULTS = { to: 'right', duration: 700, 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 };
}
// Where to end: an x/y offset that puts the element just outside the box.
function offset(el, to) {
var d = +el.dataset.distance;
var r = el.getBoundingClientRect();
var c = clipBox(el);
if (to === 'left') return { x: -(d || r.right - c.left + 8), y: 0 };
if (to === 'top') return { x: 0, y: -(d || r.bottom - c.top + 8) };
if (to === 'bottom') return { x: 0, y: d || c.bottom - r.top + 8 };
return { x: d || c.right - r.left + 8, y: 0 };
}
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;
}
anime.utils.set(el, { x: 0, y: 0 });
var o = offset(el, to);
var props = {
x: o.x,
y: o.y,
duration: t,
ease: 'in(3)',
onComplete: function () {
if (done) done();
}
};
// Fade only when it stops short of the edge.
if (d.distance) props.opacity = { to: 0, delay: t * 0.3, duration: t * 0.7, ease: 'in(2)' };
anime.animate(el, props);
}
function cycle(el) {
var d = el.dataset;
var s = el._slo;
var tos = (d.to || DEFAULTS.to).split(/[\s,]+/);
var loop = d.loop !== undefined && d.loop !== 'false';
leave(el, tos[s.n++ % tos.length], function () {
if (!loop) {
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, 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.slideOut = 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._slo;
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-slide-out]').forEach(function (el) {
if (el._slo) return;
el._slo = { n: 0 };
if (!reduced()) io.observe(el);
});
}
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', init);
else init();
})();
</script><div class="toast" data-slide-out>Conversation archived</div>
<!-- or on demand, when a close button is clicked: -->
<script>
button.addEventListener('click', function () {
animacious.slideOut(toast, function () { toast.remove(); });
});
</script>