← Entrances

Hinge

The element comes loose at one top corner and swings down on the other, like a sign hanging by its last screw. It sways a couple of times, then drops away off the bottom.

Offer expiredFree delivery weekend

How to use it

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

Options

Add any of these attributes to the same element.

data-cornerThe top corner it hangs from: left or right. Give both, left right, and each loop uses the next. Default left.
data-angleHow far it swings down, in degrees. Default 80.
data-durationHow long the whole swing and drop takes. Default 2000 ms.
data-delayHow long it stays in view before it comes loose. Default 1200 ms.
data-loopFade back in, hold and come loose again.
data-holdHow long it stays between exits, when looping. Default 1400 ms.

It drops past the bottom of the nearest container that clips its overflow (overflow: hidden or clip), or of the window, so put it in a clipping wrapper to keep it from making the page scroll. The script sets transform-origin on the element while it swings. It ends hidden (visibility: hidden) but still taking up its space. To play it when you choose, leave the attribute off and call animacious.hinge(el).

1 · Snippet
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/bundles/anime.umd.min.js"></script>
<script>
  (function () {
    // Hinge: the element comes loose at one top corner, swings on the other
    // like a sign hanging by its last screw, then drops away off the bottom.
    // Usage: <div data-hinge>…</div>
    // It plays once the element has been in view for data-delay ms. It drops
    // 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-corner (the corner it hangs from, "left", default, or
    // "right"; give both, "left right", and each loop uses the next),
    // data-angle (how far it swings down, in degrees, default 80),
    // data-duration (ms, default 2000), data-delay (ms in view before it
    // leaves, default 1200), data-loop (fade back in, hold and come loose
    // again), data-hold (ms to stay between exits when looping, default 1400).
    // Script use: animacious.hinge(el, done) plays it on any element now,
    // using its data-* options, and calls done afterwards.
    var DEFAULTS = { corner: 'left', angle: 80, duration: 2000, 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, corner, done) {
      var d = el.dataset;
      var t = +d.duration || DEFAULTS.duration;
      var a = (d.angle === undefined ? DEFAULTS.angle : +d.angle || 0) * (corner === 'right' ? -1 : 1);
      if (reduced()) {
        anime.utils.set(el, { opacity: 0 });
        if (done) done();
        return;
      }
      anime.utils.set(el, { x: 0, y: 0, rotate: 0 });
      el.style.transformOrigin = (corner === 'right' ? 'right' : 'left') + ' top';
      // The pivot is the top corner, so once it is past the edge so is the rest.
      var fall = clipBottom(el) - el.getBoundingClientRect().top + 8;
      anime.createTimeline({
        onComplete: function () {
          if (done) done();
        }
      })
        .add(el, { rotate: a, duration: t * 0.2, ease: 'inOut(2)' })
        .add(el, { rotate: a * 0.75, duration: t * 0.13, ease: 'out(2)' })
        .add(el, { rotate: a, duration: t * 0.13, ease: 'in(2)' })
        .add(el, { rotate: a * 0.86, duration: t * 0.1, ease: 'out(2)' })
        .add(el, { rotate: a, duration: t * 0.1, ease: 'in(2)' })
        .add(el, {
          y: { to: fall, ease: 'in(2)' },
          opacity: { to: 0, delay: t * 0.2, duration: t * 0.14, ease: 'in(2)' },
          duration: t * 0.34
        });
    }

    function cycle(el) {
      var d = el.dataset;
      var s = el._hng;
      var corners = (d.corner || DEFAULTS.corner).split(/[\s,]+/);
      leave(el, corners[s.n++ % corners.length], 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, { x: 0, y: 0, rotate: 0, opacity: 0 });
          el.style.transformOrigin = '';
          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.hinge = function (el, done) {
      leave(el, (el.dataset.corner || DEFAULTS.corner).split(/[\s,]+/)[0], 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._hng;
          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-hinge]').forEach(function (el) {
        if (el._hng) return;
        el._hng = { 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-hinge>Offer expired</div>

<!-- or hanging from the right corner, swinging less: -->
<div class="card" data-hinge data-corner="right" data-angle="60">Offer expired</div>