← Entrances

Rotate In

The element swings into place round one of its corners, as if pinned there, fading in as it turns. Pick the corner, or give several and a looping demo takes each in turn.

Tickets bookedRow F · Seats 12–13

How to use it

  1. Paste the snippet just before </head>.
  2. Add data-rotate-in to any element.
  3. Choose a corner with data-corner if it shouldn’t turn round its bottom left.

Options

Add any of these attributes to the same element.

data-cornerThe corner it turns round: bottom-left, bottom-right, top-left or top-right. Bottom corners swing it down from above, top corners up from below. Give several, like bottom-left bottom-right, and each loop uses the next. Default bottom-left.
data-angleHow far it turns, in degrees. Negative turns the other way. Default 90.
data-durationHow long the turn takes. Default 900 ms.
data-delayWait before the first entrance. Default 0 ms.
data-loopHold, fade away and rotate in again.
data-holdHow long it stays before fading, when looping. Default 1400 ms.

The script sets transform-origin on the element to the corner it turns round.

1 · Snippet
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/bundles/anime.umd.min.js"></script>
<script>
  (function () {
    // Rotate In: the element swings into place round one of its corners, as
    // if pinned there, fading in as it turns.
    // Usage: <div data-rotate-in>…</div>
    // Optional: data-corner (the corner it turns round: "bottom-left",
    // default, "bottom-right", "top-left" or "top-right"; give several, like
    // "bottom-left bottom-right", and each loop uses the next), data-angle
    // (how far it turns, in degrees, default 90; negative turns the other
    // way), data-duration (ms, default 900), data-delay (ms before the first
    // entrance), data-loop (hold, fade away and rotate in again), data-hold
    // (ms to stay when looping, default 1400).
    var DEFAULTS = { corner: 'bottom-left', angle: 90, duration: 900, delay: 0, hold: 1400 };

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

    // "bottom-left" (or "left-bottom") -> the CSS origin and which way it turns.
    function corner(name) {
      var top = /top/.test(name);
      var right = /right/.test(name);
      return {
        origin: (right ? 'right' : 'left') + ' ' + (top ? 'top' : 'bottom'),
        // Bottom corners swing down into place from above; top corners swing
        // up from below.
        sign: (top ? 1 : -1) * (right ? -1 : 1)
      };
    }

    function enter(el, name, done) {
      var d = el.dataset;
      var t = +d.duration || DEFAULTS.duration;
      var a = d.angle === undefined ? DEFAULTS.angle : +d.angle || 0;
      var c = corner(name);
      el.style.transformOrigin = c.origin;
      anime.utils.set(el, { rotate: a * c.sign, opacity: 0 });
      anime.animate(el, {
        rotate: { to: 0, ease: 'outBack(1.1)' },
        opacity: { to: 1, duration: t * 0.5, ease: 'out(2)' },
        duration: t,
        onComplete: done
      });
    }

    function cycle(el) {
      var d = el.dataset;
      var s = el._rti;
      var corners = (d.corner || DEFAULTS.corner).split(/[\s,]+/);
      enter(el, corners[s.n++ % corners.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._rti;
          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-rotate-in]').forEach(function (el) {
        if (el._rti) return;
        el._rti = { 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-rotate-in>Tickets booked</div>

<!-- or round its bottom right corner, turning less: -->
<div class="card" data-rotate-in data-corner="bottom-right" data-angle="45">Tickets booked</div>