← Entrances

Blur Out

The element drifts out of focus, blurring more and more as it fades away to nothing. It stays the same size, or let it swell or shrink a little as it goes.

Notification clearedYour parcel is on its way

How to use it

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

Options

Add any of these attributes to the same element.

data-blurHow blurred it ends, in px. Default 20.
data-scaleThe size it ends at, like 1.1 to swell a little as it goes. Default 1, the same size.
data-durationHow long it takes to leave. Default 800 ms.
data-delayHow long it stays in view before it leaves. Default 1200 ms.
data-loopFade back in, hold and blur 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.blurOut(el). The blur uses a CSS filter, which the script clears when it fades back in.

1 · Snippet
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/bundles/anime.umd.min.js"></script>
<script>
  (function () {
    // Blur Out: the element goes out of focus, blurring more and more as it
    // fades to nothing. It stays the same size unless you give data-scale.
    // Usage: <div data-blur-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-blur (how blurred it ends, in px, default 20),
    // data-scale (the size it ends at, default 1, so no zoom),
    // data-duration (ms, default 800), data-delay (ms in view before it
    // leaves, default 1200), data-loop (fade back in, hold and blur out
    // again), data-hold (ms to stay between exits when looping, default 1400).
    // Script use: animacious.blurOut(el, done) plays it on any element now,
    // using its data-* options, and calls done afterwards.
    var DEFAULTS = { blur: 20, scale: 1, duration: 800, 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;
      var b = d.blur === undefined ? DEFAULTS.blur : +d.blur || 0;
      if (reduced()) {
        anime.utils.set(el, { opacity: 0 });
        if (done) done();
        return;
      }
      anime.utils.set(el, { filter: 'blur(0px)' });
      anime.animate(el, {
        filter: { to: 'blur(' + b + 'px)', ease: 'in(2)' },
        scale: { to: +d.scale || DEFAULTS.scale, ease: 'in(2)' },
        opacity: { to: 0, delay: t * 0.35, duration: t * 0.65, ease: 'in(2)' },
        duration: t,
        onComplete: function () {
          if (done) done();
        }
      });
    }

    function cycle(el) {
      var d = el.dataset;
      var s = el._blo;
      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 });
          el.style.filter = '';
          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.blurOut = 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._blo;
          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-blur-out]').forEach(function (el) {
        if (el._blo) return;
        el._blo = {};
        if (!reduced()) io.observe(el);
      });
    }

    if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', init);
    else init();
  })();
</script>
2 · Your HTML
<div class="card" data-blur-out>Notification cleared</div>

<!-- or on demand, removing it afterwards: -->
<script>
  animacious.blurOut(card, function () { card.remove(); });
</script>