← Entrances

Drop Out

The element gives a tiny hop, then falls off the bottom of the screen, speeding up under gravity and tipping a little as it goes.

Photo deletedIMG_2041.jpg

How to use it

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

Options

Add any of these attributes to the same element.

data-tiltHow far it tips as it falls, in degrees. A looping demo tips the other way each time. Default 8.
data-hopHow far it lifts before it falls, in px. 0 for none. Default 10.
data-distanceFall this many px, fading out, instead of leaving the screen.
data-durationHow long the fall takes. Default 750 ms.
data-delayHow long it stays in view before it drops. Default 1200 ms.
data-loopFade back in, hold and drop out again.
data-holdHow long it stays between exits, when looping. Default 1400 ms.

It leaves past the edge of the nearest container that clips its overflow (overflow: hidden or clip), or of the window. Put it in a clipping wrapper, or give the page overflow-x: clip, so it can’t make the page scroll sideways on its way out. It ends hidden (visibility: hidden) but still taking up its space. To play it when you choose, leave the attribute off and call animacious.dropOut(el).

1 · Snippet
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/bundles/anime.umd.min.js"></script>
<script>
  (function () {
    // Drop Out: the element gives a tiny hop, then falls off the bottom of the
    // screen under gravity, tipping slightly as it goes.
    // Usage: <div data-drop-out>…</div>
    // It plays once the element has been in view for data-delay ms. It falls
    // past the bottom of the nearest container that clips its overflow
    // (overflow: hidden or clip), or of the window, and ends hidden
    // (visibility: hidden) but still taking up its space.
    // Optional: data-tilt (how far it tips as it falls, in degrees, default 8;
    // a looping demo tips the other way each time), data-hop (px it lifts
    // before falling, default 10, 0 for none), data-distance (px to fall,
    // fading out, instead of leaving the screen), data-duration (ms for the
    // fall, default 750), data-delay (ms in view before it leaves, default
    // 1200), data-loop (fade back in, hold and drop out again), data-hold (ms
    // to stay between exits when looping, default 1400).
    // Script use: animacious.dropOut(el, done) plays it on any element now,
    // using its data-* options, and calls done afterwards.
    var DEFAULTS = { tilt: 8, hop: 10, duration: 750, delay: 1200, hold: 1400 };

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

    // The bottom edge the element must fall past to be "off screen".
    function clipBottom(el) {
      for (var p = el.parentElement; p && p !== document.body; p = p.parentElement) {
        var s = getComputedStyle(p);
        if (s.overflowX !== 'visible' || s.overflowY !== 'visible') return p.getBoundingClientRect().bottom;
      }
      return window.innerHeight;
    }

    function leave(el, dir, done) {
      var d = el.dataset;
      var t = +d.duration || DEFAULTS.duration;
      var tilt = (d.tilt === undefined ? DEFAULTS.tilt : +d.tilt || 0) * dir;
      var hop = d.hop === undefined ? DEFAULTS.hop : +d.hop || 0;
      if (reduced()) {
        anime.utils.set(el, { opacity: 0 });
        if (done) done();
        return;
      }
      anime.utils.set(el, { y: 0, rotate: 0 });
      var r = el.getBoundingClientRect();
      // Clear the edge even with the tilt lifting one corner.
      var fall = +d.distance || clipBottom(el) - r.top + r.width * Math.abs(Math.sin(tilt * Math.PI / 180)) + 8;
      var tl = anime.createTimeline({
        onComplete: function () {
          if (done) done();
        }
      });
      if (hop) tl.add(el, { y: -hop, rotate: -tilt * 0.25, duration: 220, ease: 'out(2)' });
      var fallProps = {
        y: { to: fall, ease: 'in(2)' },
        rotate: { to: tilt, ease: 'in(1.5)' },
        duration: t
      };
      if (d.distance) fallProps.opacity = { to: 0, delay: t * 0.3, duration: t * 0.7, ease: 'in(2)' };
      tl.add(el, fallProps);
    }

    function cycle(el) {
      var d = el.dataset;
      var s = el._dro;
      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, { y: 0, rotate: 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.dropOut = 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._dro;
          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-drop-out]').forEach(function (el) {
        if (el._dro) return;
        el._dro = { 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-drop-out>Photo deleted</div>

<!-- or a straight drop with no hop or tilt: -->
<div class="card" data-drop-out data-hop="0" data-tilt="0">Photo deleted</div>