← Entrances

Blur In

The element sharpens out of a heavy blur as it fades in, like a camera pulling focus. It stays the same size, or give it a gentle zoom as well.

Photo readyBeach at sunset.jpg

How to use it

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

Options

Add any of these attributes to the same element.

data-blurHow blurred it starts, in px. Default 20.
data-scaleThe size it starts at, for a little zoom as it sharpens, like 1.1. Default 1, the same size.
data-durationHow long it takes to sharpen. Default 900 ms.
data-delayWait before the first entrance. Default 0 ms.
data-loopHold, fade away and sharpen in again.
data-holdHow long it stays before fading, when looping. Default 1400 ms.

The blur is a CSS filter, which the script clears once the element is sharp so text stays crisp.

1 · Snippet
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/bundles/anime.umd.min.js"></script>
<script>
  (function () {
    // Blur In: the element sharpens out of a heavy blur as it fades in, like a
    // camera pulling focus. It stays the same size unless you give data-scale.
    // Usage: <div data-blur-in>…</div>
    // Optional: data-blur (how blurred it starts, in px, default 20),
    // data-scale (the size it starts at, default 1, so no zoom),
    // data-duration (ms, default 900), data-delay (ms before the first
    // entrance), data-loop (hold, fade away and sharpen in again), data-hold
    // (ms to stay when looping, default 1400).
    var DEFAULTS = { blur: 20, scale: 1, duration: 900, delay: 0, hold: 1400 };

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

    function enter(el, done) {
      var d = el.dataset;
      var t = +d.duration || DEFAULTS.duration;
      var b = d.blur === undefined ? DEFAULTS.blur : +d.blur || 0;
      anime.utils.set(el, { scale: +d.scale || DEFAULTS.scale, opacity: 0, filter: 'blur(' + b + 'px)' });
      anime.animate(el, {
        filter: { to: 'blur(0px)', ease: 'out(3)' },
        scale: { to: 1, ease: 'out(3)' },
        opacity: { to: 1, duration: t * 0.45, ease: 'out(2)' },
        duration: t,
        onComplete: function () {
          el.style.filter = '';   // drop the finished filter so text stays crisp
          done();
        }
      });
    }

    function cycle(el) {
      var d = el.dataset;
      var s = el._bli;
      enter(el, 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._bli;
          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-blur-in]').forEach(function (el) {
        if (el._bli) return;
        el._bli = {};
        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-blur-in>Photo ready</div>

<!-- or blurrier, settling from slightly large: -->
<div class="card" data-blur-in data-blur="32" data-scale="1.1">Photo ready</div>