← Entrances

Swirl In

The element spins in as it grows, from a dot to full size, slowing its turn as it arrives. A looping demo spins the other way each time.

Sync completeAll 128 files up to date

How to use it

  1. Paste the snippet just before </head>.
  2. Add data-swirl-in 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 starts at. Default 0.
data-durationHow long the swirl takes. Default 1000 ms.
data-delayWait before the first entrance. Default 0 ms.
data-loopHold, fade away and swirl in again, the other way round.
data-holdHow long it stays before fading, when looping. Default 1400 ms.
1 · Snippet
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/bundles/anime.umd.min.js"></script>
<script>
  (function () {
    // Swirl In: the element spins in as it grows, from a dot to full size,
    // slowing its turn as it arrives.
    // Usage: <div data-swirl-in>…</div>
    // Optional: data-turns (how many turns it makes, default 1.5; negative
    // spins anticlockwise), data-scale (the size it starts at, default 0),
    // data-duration (ms, default 1000), data-delay (ms before the first
    // entrance), data-loop (hold, fade away and swirl in again; each loop
    // spins the other way), data-hold (ms to stay when looping, default 1400).
    var DEFAULTS = { turns: 1.5, scale: 0, duration: 1000, delay: 0, hold: 1400 };

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

    function enter(el, dir, done) {
      var d = el.dataset;
      var t = +d.duration || DEFAULTS.duration;
      var turns = d.turns === undefined ? DEFAULTS.turns : +d.turns || 0;
      anime.utils.set(el, {
        rotate: -360 * turns * dir,
        scale: d.scale === undefined ? DEFAULTS.scale : +d.scale,
        opacity: 0
      });
      anime.animate(el, {
        rotate: { to: 0, ease: 'out(3)' },
        scale: { to: 1, ease: 'out(4)' },
        opacity: { to: 1, duration: t * 0.4, ease: 'out(2)' },
        duration: t,
        onComplete: done
      });
    }

    function cycle(el) {
      var d = el.dataset;
      var s = el._swi;
      enter(el, s.n++ % 2 ? -1 : 1, 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._swi;
          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-swirl-in]').forEach(function (el) {
        if (el._swi) return;
        el._swi = { 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>
2 · Your HTML
<div class="card" data-swirl-in>Sync complete</div>

<!-- or a single anticlockwise turn: -->
<div class="card" data-swirl-in data-turns="-1">Sync complete</div>