← Entrances

Zoom Out

The element shrinks and fades away. Shrink it into a corner, or set a size above 1 and it grows as it fades instead.

Upload complete3 photos added

How to use it

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

Options

Add any of these attributes to the same element.

data-scaleThe size it shrinks to. Above 1 it grows as it fades. Default 0.3.
data-originThe point it shrinks towards, as a CSS transform-origin like right top. Default the centre.
data-durationHow long it takes to leave. Default 600 ms.
data-delayHow long it stays in view before it leaves. Default 1200 ms.
data-loopFade back in, hold and zoom 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.zoomOut(el).

1 · Snippet
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/bundles/anime.umd.min.js"></script>
<script>
  (function () {
    // Zoom Out: the element shrinks and fades away.
    // Usage: <div data-zoom-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-scale (the size it shrinks to, default 0.3; above 1 it
    // grows and fades instead), data-origin (the point it shrinks towards,
    // any CSS transform-origin like "right top", default the centre),
    // data-duration (ms, default 600), data-delay (ms in view before it
    // leaves, default 1200), data-loop (fade back in, hold and zoom out
    // again), data-hold (ms to stay between exits when looping, default 1400).
    // Script use: animacious.zoomOut(el, done) plays it on any element now,
    // using its data-* options, and calls done afterwards.
    var DEFAULTS = { scale: 0.3, duration: 600, delay: 1200, hold: 1400 };

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

    function leave(el, done) {
      var d = el.dataset;
      var t = +d.duration || DEFAULTS.duration;
      if (reduced()) {
        anime.utils.set(el, { opacity: 0 });
        if (done) done();
        return;
      }
      if (d.origin) el.style.transformOrigin = d.origin;
      anime.animate(el, {
        scale: { to: d.scale === undefined ? DEFAULTS.scale : +d.scale, ease: 'in(2)' },
        opacity: { to: 0, delay: t * 0.15, duration: t * 0.85, ease: 'in(1.5)' },
        duration: t,
        onComplete: function () {
          if (done) done();
        }
      });
    }

    function cycle(el) {
      var d = el.dataset;
      var s = el._zmo;
      leave(el, 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, { 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.zoomOut = function (el, done) {
      leave(el, 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._zmo;
          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-zoom-out]').forEach(function (el) {
        if (el._zmo) return;
        el._zmo = { 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-zoom-out>Upload complete</div>

<!-- or growing as it fades: -->
<div class="card" data-zoom-out data-scale="1.5">Upload complete</div>