← Entrances

Unroll In

The element unrolls like a scroll or banner. A roller runs from its top edge to the bottom, uncovering it as it goes, while a second rod stays put at the top. Unroll it sideways instead, and keep the rods or let them fade.

NoticeGrand openingSaturday · 10 am

How to use it

  1. Paste the snippet just before </head>.
  2. Add data-unroll-in to any element.
  3. Add data-keep if the rods should stay, like a hanging scroll.

Options

Add any of these attributes to the same element.

data-directiondown unrolls from the top edge, right from the left edge. Give both, down right, and each loop uses the next. Default down.
data-colorThe rods’ colour. Default a wood brown, #8b5a2b.
data-sizeHow thick the rods are, in px. Default 14.
data-keepLeave the rods in place afterwards. Without it they fade away.
data-durationHow long the unroll takes. Default 1200 ms.
data-delayWait before the first entrance. Default 0 ms.
data-loopHold, fade away and unroll again.
data-holdHow long it stays before fading, when looping. Default 1600 ms.

The rods are drawn inside the element and poke out a little past its sides, so the script gives it position: relative if it has no position. It uses clip-path while it unrolls, and removes it afterwards.

1 · Snippet
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/bundles/anime.umd.min.js"></script>
<script>
  (function () {
    // Unroll In: the element unrolls like a scroll or banner. A roller runs
    // from one edge to the other, uncovering the element as it goes, while a
    // rod stays put at the edge it started from.
    // Usage: <div data-unroll-in>…</div>
    // The rods are drawn inside the element, so the script gives it
    // position: relative if it has no position of its own.
    // Optional: data-direction ("down", default, unrolls from the top edge;
    // "right" from the left edge; give both, "down right", and each loop uses
    // the next), data-color (the rods' colour, default a wood brown), data-size
    // (rod thickness in px, default 14), data-keep (leave the rods in place
    // afterwards, like a hanging scroll; without it they fade away),
    // data-duration (ms, default 1200), data-delay (ms before the first
    // entrance), data-loop (hold, fade away and unroll again), data-hold (ms to
    // stay when looping, default 1600).
    var DEFAULTS = { direction: 'down', color: '#8b5a2b', size: 14, duration: 1200, delay: 0, hold: 1600 };

    var css =
      '.unr-rod{position:absolute;z-index:2;pointer-events:none;border-radius:999px;' +
      'background:linear-gradient(var(--unr-a),rgba(0,0,0,.3),rgba(255,255,255,.4) 30%,rgba(255,255,255,0) 55%,rgba(0,0,0,.35)),var(--unr-c);' +
      'box-shadow:0 2px 5px rgba(0,0,0,.25)}' +
      '.unr-rod::before,.unr-rod::after{content:"";position:absolute;border-radius:50%;background:inherit;box-shadow:inherit}' +
      // Round knobs on the ends of each rod.
      '.unr-down::before,.unr-down::after{top:-25%;height:150%;aspect-ratio:1}' +
      '.unr-down::before{left:-2px}.unr-down::after{right:-2px}' +
      '.unr-right::before,.unr-right::after{left:-25%;width:150%;aspect-ratio:1}' +
      '.unr-right::before{top:-2px}.unr-right::after{bottom:-2px}';
    var style = document.createElement('style');
    style.textContent = css;
    document.head.appendChild(style);

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

    function rod(el, dir, size, color) {
      var r = document.createElement('span');
      r.className = 'unr-rod unr-' + dir;
      r.setAttribute('aria-hidden', 'true');
      r.style.setProperty('--unr-c', color);
      r.style.setProperty('--unr-a', dir === 'down' ? 'to bottom' : 'to right');
      // Rods poke out a little past the sides, like the ends of a scroll.
      var over = Math.round(size * 0.6);
      if (dir === 'down') {
        r.style.left = -over - el.clientLeft + 'px';
        r.style.right = -over - el.clientLeft + 'px';
        r.style.top = -size / 2 - el.clientTop + 'px';
        r.style.height = size + 'px';
      } else {
        r.style.top = -over - el.clientTop + 'px';
        r.style.bottom = -over - el.clientTop + 'px';
        r.style.left = -size / 2 - el.clientLeft + 'px';
        r.style.width = size + 'px';
      }
      el.appendChild(r);
      return r;
    }

    function clear(el) {
      var old = el.querySelectorAll(':scope > .unr-rod');
      for (var i = 0; i < old.length; i++) old[i].remove();
      el.style.clipPath = '';
    }

    function enter(el, dir, done) {
      var d = el.dataset;
      var t = +d.duration || DEFAULTS.duration;
      var size = +d.size || DEFAULTS.size;
      var color = d.color || DEFAULTS.color;
      dir = dir === 'right' ? 'right' : 'down';
      clear(el);
      if (getComputedStyle(el).position === 'static') el.style.position = 'relative';
      var len = dir === 'down' ? el.offsetHeight : el.offsetWidth;
      var fixed = rod(el, dir, size, color);
      var roller = rod(el, dir, size, color);
      var pad = size + 4;
      var over = Math.round(size * 0.6) + 4;
      var p = { at: 0 };
      function draw() {
        // Uncover everything up to the roller's middle; the roller hides the seam.
        var hide = Math.max(0, len - p.at - size / 2);
        el.style.clipPath = dir === 'down'
          ? 'inset(-' + pad + 'px -' + over + 'px ' + hide + 'px -' + over + 'px)'
          : 'inset(-' + over + 'px ' + hide + 'px -' + over + 'px -' + pad + 'px)';
        anime.utils.set(roller, dir === 'down' ? { y: p.at } : { x: p.at });
      }
      draw();
      anime.utils.set(el, { opacity: 1 });
      anime.utils.set([fixed, roller], { opacity: 0 });
      anime.animate([fixed, roller], { opacity: 1, duration: 200, ease: 'out(2)' });
      anime.animate(p, {
        at: len,
        delay: 150,
        duration: t,
        ease: 'out(3)',
        onUpdate: draw,
        onComplete: function () {
          el.style.clipPath = '';
          if (d.keep !== undefined && d.keep !== 'false') return done();
          anime.animate([fixed, roller], {
            opacity: 0,
            duration: 350,
            ease: 'out(2)',
            onComplete: function () {
              fixed.remove();
              roller.remove();
            }
          });
          done();
        }
      });
    }

    function cycle(el) {
      var d = el.dataset;
      var s = el._unr;
      var dirs = (d.direction || DEFAULTS.direction).split(/[\s,]+/);
      enter(el, dirs[s.n++ % dirs.length], function () {
        if (d.loop === undefined || d.loop === 'false') return;
        setTimeout(function () {
          anime.animate(el, {
            opacity: 0,
            duration: 400,
            ease: 'out(2)',
            onComplete: function () {
              clear(el);
              // 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._unr;
          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-unroll-in]').forEach(function (el) {
        if (el._unr) return;
        el._unr = { n: 0 };
        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="notice" data-unroll-in>Grand opening</div>

<!-- or unrolling sideways, keeping red rods: -->
<div class="notice" data-unroll-in data-direction="right" data-color="#b91c1c" data-keep>Grand opening</div>