Slide In
The element slides in from fully off screen and eases to a stop in its place. Pick the edge it comes from, or give several and a looping demo takes each in turn.
New messageSam: See you at six?
How to use it
- Paste the snippet just before
</head>. - Add
data-slide-into any element. - Choose an edge with
data-fromif it shouldn’t come from the left.
Options
Add any of these attributes to the same element.
data-from | The edge it comes from: left, right, top or bottom. Give several, like left top right, and each loop uses the next. Default left. |
data-distance | Travel this many px instead of starting fully off screen. |
data-duration | How long the slide takes. Default 900 ms. |
data-delay | Wait before the first entrance. Default 0 ms. |
data-loop | Hold, fade away and slide in again. |
data-hold | How long it stays before fading, when looping. Default 1400 ms. |
It starts just past the edge of the nearest container that clips its overflow (overflow: hidden or clip), or of the window if there isn’t one. Put it in a clipping wrapper, or give the page overflow-x: clip, so it can’t make the page scroll sideways while it’s off screen.
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/bundles/anime.umd.min.js"></script>
<script>
(function () {
// Slide In: the element slides in from fully off screen, from any edge,
// and eases to a stop in its place.
// Usage: <div data-slide-in>…</div>
// It starts just past the edge of the nearest container that clips its
// overflow (overflow: hidden or clip), or of the window if there is none.
// Optional: data-from ("left", default, "right", "top" or "bottom"; give
// several, like "left top right", and each loop uses the next),
// data-distance (px to travel instead of from off screen), data-duration
// (ms, default 900), data-delay (ms before the first entrance), data-loop
// (hold, fade away and slide in again), data-hold (ms to stay when
// looping, default 1400).
var DEFAULTS = { from: 'left', duration: 900, delay: 0, 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 start: an x/y offset that puts the element just outside the box.
function offset(el, from) {
var d = +el.dataset.distance;
var r = el.getBoundingClientRect();
var c = clipBox(el);
if (from === 'right') return { x: d || c.right - r.left + 8, y: 0 };
if (from === 'top') return { x: 0, y: -(d || r.bottom - c.top + 8) };
if (from === 'bottom') return { x: 0, y: d || c.bottom - r.top + 8 };
return { x: -(d || r.right - c.left + 8), y: 0 };
}
function enter(el, from, done) {
var o = offset(el, from);
anime.utils.set(el, { x: o.x, y: o.y, opacity: 1 });
anime.animate(el, {
x: 0,
y: 0,
duration: +el.dataset.duration || DEFAULTS.duration,
ease: 'out(4)',
onComplete: done
});
}
function cycle(el) {
var d = el.dataset;
var s = el._sli;
var froms = (d.from || DEFAULTS.from).split(/[\s,]+/);
enter(el, froms[s.n++ % froms.length], function () {
if (d.loop === undefined || d.loop === 'false') return;
setTimeout(function () {
anime.animate(el, {
opacity: 0,
duration: 400,
ease: 'out(2)',
onComplete: function () {
// Wait until it's back on screen before the next entrance.
setTimeout(function () {
if (s.visible) cycle(el);
else s.waiting = true;
}, 500);
}
});
}, +d.hold || DEFAULTS.hold);
});
}
function init() {
var io = new IntersectionObserver(function (entries) {
entries.forEach(function (e) {
var s = e.target._sli;
s.visible = e.isIntersecting;
if (!e.isIntersecting) return;
if (!s.started) {
s.started = true;
setTimeout(function () { cycle(e.target); }, +e.target.dataset.delay || DEFAULTS.delay);
} else if (s.waiting) {
s.waiting = false;
cycle(e.target);
}
});
}, { threshold: 0.2 });
document.querySelectorAll('[data-slide-in]').forEach(function (el) {
if (el._sli) return;
el._sli = { n: 0 };
if (reduced()) return;
anime.utils.set(el, { opacity: 0 });
io.observe(el);
});
}
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', init);
else init();
})();
</script><div class="toast" data-slide-in>New message</div>
<!-- or from the right, sliding just 120px: -->
<div class="toast" data-slide-in data-from="right" data-distance="120">New message</div>