Roll In
The element rolls in from the side, turning like a wheel as it travels and slowing to a stop in its place. It turns exactly as far as a wheel of its size would, so round things look right.
New
How to use it
- Paste the snippet just before
</head>. - Add
data-roll-into any element. Round ones, like badges and avatars, look the most natural.
Options
Add any of these attributes to the same element.
data-from | The side it rolls in from: left or right. Give both, left right, and each loop uses the next. Default left. |
data-distance | Roll this many px instead of starting fully off screen. |
data-turns | Make this many turns instead of turning true to the distance. |
data-duration | How long the roll takes. Default 1300 ms. |
data-delay | Wait before the first entrance. Default 0 ms. |
data-loop | Hold, fade away and roll 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 () {
// Roll In: the element rolls in from the side, turning like a wheel as it
// travels: one full turn for every circumference it covers.
// Usage: <div data-roll-in>…</div>
// Round elements look most natural. It starts just past the edge of the
// nearest container that clips its overflow (overflow: hidden or clip),
// or of the window.
// Optional: data-from ("left", default, or "right"; give both, "left
// right", and each loop uses the next), data-distance (px to roll instead
// of from off screen), data-turns (turns to make instead of rolling true
// to the distance), data-duration (ms, default 1300), data-delay (ms
// before the first entrance), data-loop (hold, fade away and roll in
// again), data-hold (ms to stay when looping, default 1400).
var DEFAULTS = { from: 'left', duration: 1300, 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 };
}
function enter(el, from, done) {
var d = el.dataset;
var t = +d.duration || DEFAULTS.duration;
var r = el.getBoundingClientRect();
var c = clipBox(el);
var dir = from === 'right' ? 1 : -1;
var dist = +d.distance || (dir > 0 ? c.right - r.left : r.right - c.left) + 8;
// A wheel turns one radian for every radius it rolls.
var radius = Math.min(r.width, r.height) / 2 || 1;
var deg = d.turns ? +d.turns * 360 : dist / radius * 180 / Math.PI;
anime.utils.set(el, { x: dir * dist, rotate: dir * deg, opacity: 0 });
anime.animate(el, {
x: { to: 0, ease: 'out(3)' },
rotate: { to: 0, ease: 'out(3)' },
opacity: { to: 1, duration: t * 0.25, ease: 'linear' },
duration: t,
onComplete: done
});
}
function cycle(el) {
var d = el.dataset;
var s = el._rli;
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._rli;
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-roll-in]').forEach(function (el) {
if (el._rli) return;
el._rli = { 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="badge" data-roll-in>New</div>
<!-- or from the right, with exactly one turn: -->
<div class="badge" data-roll-in data-from="right" data-turns="1">New</div>