← Entrances

Swirl Out

The element spins away as it shrinks to a dot, turning faster as it goes. A looping demo spins the other way each time.

Removed from favouritesLisbon city guide

How to use it

  1. Paste the snippet just before </head>.
  2. Add data-swirl-out to any element.

Options

Add any of these attributes to the same element.

data-turnsHow many turns it makes. Negative spins anticlockwise. Default 1.5.
data-scaleThe size it shrinks to. Default 0.
data-durationHow long the swirl takes. Default 900 ms.
data-delayHow long it stays in view before it leaves. Default 1200 ms.
data-loopFade back in, hold and swirl out again, the other way round.
data-holdHow long it stays between exits, when looping. Default 1400 ms.

It plays once it has been in view for data-delay, and 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.swirlOut(el).

1 · Snippet
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/bundles/anime.umd.min.js"></script>
<script>
  (function () {
    // Swirl Out: the element spins away as it shrinks to a dot, picking up
    // speed as it goes. A looping demo spins the other way each time.
    // Usage: <div data-swirl-out>…</div>
    // It plays once the element has been in view for data-delay ms, and ends
    // hidden (visibility: hidden) but still taking up its space.
    // Optional: data-turns (how many turns, negative for anticlockwise,
    // default 1.5), data-scale (the size it shrinks to, default 0),
    // data-duration (ms, default 900), data-delay (ms in view before it
    // leaves, default 1200), data-loop (fade back in, hold and swirl out
    // again, the other way round), data-hold (ms to stay between exits when
    // looping, default 1400).
    // Script use: animacious.swirlOut(el, done) plays it on any element now,
    // using its data-* options, and calls done afterwards.
    var DEFAULTS = { turns: 1.5, scale: 0, duration: 900, delay: 1200, hold: 1400 };

    function reduced() {
      return window.matchMedia('(prefers-reduced-motion: reduce)').matches;
    }

    function leave(el, dir, done) {
      var d = el.dataset;
      var t = +d.duration || DEFAULTS.duration;
      var turns = d.turns === undefined ? DEFAULTS.turns : +d.turns || 0;
      if (reduced()) {
        anime.utils.set(el, { opacity: 0 });
        if (done) done();
        return;
      }
      anime.utils.set(el, { rotate: 0, scale: 1 });
      anime.animate(el, {
        rotate: { to: turns * 360 * dir, ease: 'in(2)' },
        scale: { to: d.scale === undefined ? DEFAULTS.scale : +d.scale, ease: 'in(1.6)' },
        opacity: { to: 0, delay: t * 0.45, duration: t * 0.55, ease: 'in(1.5)' },
        duration: t,
        onComplete: function () {
          if (done) done();
        }
      });
    }

    function cycle(el) {
      var d = el.dataset;
      var s = el._swo;
      var dir = s.n++ % 2 ? -1 : 1;
      leave(el, dir, 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, { rotate: 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.swirlOut = function (el, done) {
      leave(el, 1, 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._swo;
          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-swirl-out]').forEach(function (el) {
        if (el._swo) return;
        el._swo = { n: 0 };
        if (!reduced()) io.observe(el);
      });
    }

    if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', init);
    else init();
  })();
</script>
2 · Your HTML
<div class="card" data-swirl-out>Removed from favourites</div>

<!-- or a single anticlockwise turn: -->
<div class="card" data-swirl-out data-turns="-1">Removed from favourites</div>