← Entrances

Flip Out

The element tips towards you a touch, then flips away round its horizontal axis until it’s edge-on and gone. Or turn it round its vertical one.

Coupon applied10% off your order

How to use it

  1. Paste the snippet just before </head>.
  2. Add data-flip-out to any element.
  3. Add data-axis="y" to turn it sideways instead of tipping it back.

Options

Add any of these attributes to the same element.

data-axisx tips it over its horizontal axis, y turns it round its vertical one. Give both, x y, and each loop uses the next. Default x.
data-perspectiveHow close the viewer is, in px. Smaller is more dramatic. Default 800.
data-durationHow long it takes to leave. Default 750 ms.
data-delayHow long it stays in view before it leaves. Default 1200 ms.
data-loopFade back in, hold and flip out again.
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.flipOut(el).

1 · Snippet
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/bundles/anime.umd.min.js"></script>
<script>
  (function () {
    // Flip Out: the element tips towards you a touch, then flips away round
    // its horizontal or vertical axis until it's edge-on and gone.
    // Usage: <div data-flip-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-axis ("x", default, tips over the horizontal axis; "y"
    // turns round the vertical one; give both, "x y", and each loop uses the
    // next), data-perspective (px, smaller is more dramatic, default 800),
    // data-duration (ms, default 750), data-delay (ms in view before it
    // leaves, default 1200), data-loop (fade back in, hold and flip out
    // again), data-hold (ms to stay between exits when looping, default 1400).
    // Script use: animacious.flipOut(el, done) plays it on any element now,
    // using its data-* options, and calls done afterwards.
    var DEFAULTS = { axis: 'x', perspective: 800, duration: 750, delay: 1200, hold: 1400 };

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

    function leave(el, axis, done) {
      var d = el.dataset;
      var t = +d.duration || DEFAULTS.duration;
      if (reduced()) {
        anime.utils.set(el, { opacity: 0 });
        if (done) done();
        return;
      }
      var prop = axis === 'y' ? 'rotateY' : 'rotateX';
      var other = axis === 'y' ? 'rotateX' : 'rotateY';
      var start = { perspective: (+d.perspective || DEFAULTS.perspective) + 'px' };
      start[prop] = 0;
      start[other] = 0;
      anime.utils.set(el, start);
      var props = {
        opacity: { to: 0, delay: t * 0.45, duration: t * 0.55, ease: 'in(2)' },
        onComplete: function () {
          if (done) done();
        }
      };
      // A small lean towards you, then all the way round to edge-on.
      props[prop] = [
        { to: -20, duration: t * 0.3, ease: 'out(2)' },
        { to: 90, duration: t * 0.7, ease: 'in(2)' }
      ];
      anime.animate(el, props);
    }

    function cycle(el) {
      var d = el.dataset;
      var s = el._flo;
      var list = (d.axis || DEFAULTS.axis).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, { rotateX: 0, rotateY: 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.flipOut = function (el, done) {
      leave(el, (el.dataset.axis || DEFAULTS.axis).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._flo;
          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-flip-out]').forEach(function (el) {
        if (el._flo) return;
        el._flo = { 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-flip-out>Coupon applied</div>

<!-- or turning round the vertical axis: -->
<div class="card" data-flip-out data-axis="y">Coupon applied</div>