← Entrances

Roll Out

The element rolls away off the side, picking up speed and turning like a wheel. It turns exactly as far as a wheel of its size would, so round things look right.

Sale

How to use it

  1. Paste the snippet just before </head>.
  2. Add data-roll-out to any element. Round ones, like badges and avatars, look the most natural.

Options

Add any of these attributes to the same element.

data-toThe side it rolls away to: right or left. Give both, right left, and each loop uses the next. Default right.
data-distanceRoll this many px, fading out, instead of leaving the screen.
data-turnsMake this many turns instead of turning true to the distance.
data-durationHow long it takes to leave. Default 1200 ms.
data-delayHow long it stays in view before it leaves. Default 1200 ms.
data-loopFade back in, hold and roll out again.
data-holdHow 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.rollOut(el).

1 · Snippet
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/bundles/anime.umd.min.js"></script>
<script>
  (function () {
    // Roll Out: the element rolls away off the side, turning like a wheel as
    // it picks up speed: one full turn for every circumference it covers.
    // Usage: <div data-roll-out>…</div>
    // Round elements look most natural. 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, or "left"; give both, "right
    // left", and each loop uses the next), data-distance (px to roll, fading
    // out, instead of leaving the screen), data-turns (turns to make instead
    // of rolling true to the distance),
    // data-duration (ms, default 1200), data-delay (ms in view before it
    // leaves, default 1200), data-loop (fade back in, hold and roll out
    // again), data-hold (ms to stay between exits when looping, default 1400).
    // Script use: animacious.rollOut(el, done) plays it on any element now,
    // using its data-* options, and calls done afterwards.
    var DEFAULTS = { to: 'right', duration: 1200, 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;
      }
      anime.utils.set(el, { x: 0, rotate: 0 });
      var r = el.getBoundingClientRect();
      var c = clipBox(el);
      var dir = to === 'left' ? -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;
      var props = {
        x: { to: dir * dist, ease: 'in(2.5)' },
        rotate: { to: dir * deg, ease: 'in(2.5)' },
        duration: t,
        onComplete: function () {
          if (done) done();
        }
      };
      if (d.distance) props.opacity = { to: 0, delay: t * 0.4, duration: t * 0.6, ease: 'in(2)' };
      anime.animate(el, props);
    }

    function cycle(el) {
      var d = el.dataset;
      var s = el._rlo;
      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, rotate: 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.rollOut = 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._rlo;
          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-roll-out]').forEach(function (el) {
        if (el._rlo) return;
        el._rlo = { n: 0 };
        if (!reduced()) io.observe(el);
      });
    }

    if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', init);
    else init();
  })();
</script>
2 · Your HTML
<div class="badge" data-roll-out>Sale</div>

<!-- or to the left, with exactly one turn: -->
<div class="badge" data-roll-out data-to="left" data-turns="1">Sale</div>