← Attention

Nod

A “yes” nod: the element tips forward and dips down twice, the second nod a little smaller, as if agreeing with you.

Sounds good!

How to use it

  1. Paste the snippet just before </head>.
  2. Add data-nod to any element. It nods once when it scrolls into view.
  3. Add data-loop to nod again every so often.

Options

Add any of these attributes to the same element.

data-angleHow far it tips forward, in degrees. Default 22.
data-distanceHow far it dips, in px. Default 8.
data-timesHow many nods. Default 2.
data-durationHow long all the nods take. Default 1000 ms.
data-delayWait before the first nod. Default 0 ms.
data-loopNod again every so often while it’s on screen.
data-everyRest between nods, when looping. Default 1600 ms.
data-hoverAlso play it when the pointer moves over the element or it’s tapped.

Call animacious.nod(el) to make it nod from your own code, for example when a form is accepted.

1 · Snippet
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/bundles/anime.umd.min.js"></script>
<script>
  (function () {
    // Nod: a "yes" nod. The element tips forward and dips down twice, the
    // second nod a little smaller, as if agreeing with you.
    // Usage: <div data-nod>…</div>
    // Optional: data-angle (degrees it tips forward, default 22), data-distance
    // (px it dips, default 8), data-times (how many nods, default 2),
    // data-duration (ms for all the nods, default 1000), data-delay (ms before
    // the first nod), data-loop (nod again every so often while in view),
    // data-every (ms of rest between loops, default 1600), data-hover (also nod
    // on hover or tap).
    // Script use: animacious.nod(el) makes it nod from code.
    var DEFAULTS = { angle: 22, distance: 8, times: 2, duration: 1000, delay: 0, every: 1600 };

    function on(v) { return v !== undefined && v !== 'false'; }
    function reduced() { return window.matchMedia('(prefers-reduced-motion: reduce)').matches; }

    function play(el, done) {
      var s = el._nod;
      if (s.busy) { if (done) s.after = done; return; }
      if (reduced()) { if (done) done(); return; }
      s.busy = true;
      var d = el.dataset;
      function finish() {
        s.busy = false;
        var after = s.after;
        s.after = null;
        if (done) done();
        if (after) after();
      }
      var angle = d.angle === undefined ? DEFAULTS.angle : +d.angle;
      var dist = d.distance === undefined ? DEFAULTS.distance : +d.distance;
      var times = Math.max(1, Math.round(+d.times || DEFAULTS.times));
      // It tips in 3D, so the transform is written by hand with a perspective.
      var p = { a: 0, y: 0 };
      var frames = { '0%': { a: 0, y: 0 } };
      for (var i = 0; i < times; i++) {
        var start = (i / times) * 100, span = 100 / times;
        var k = 1 - i * 0.25; // each nod a little smaller
        frames[(start + span * 0.4).toFixed(2) + '%'] = { a: angle * k, y: dist * k, ease: 'out(2)' };
        frames[(start + span * 1).toFixed(2) + '%'] = { a: 0, y: 0, ease: 'inOut(2)' };
      }
      el.style.transformOrigin = '50% 30%';
      anime.animate(p, {
        keyframes: frames,
        duration: +d.duration || DEFAULTS.duration,
        onUpdate: function () {
          el.style.transform = 'perspective(500px) translateY(' + p.y.toFixed(2) + 'px) rotateX(' + (-p.a).toFixed(2) + 'deg)';
        },
        onComplete: function () {
          el.style.transform = '';
          finish();
        }
      });
    }

    function cycle(el) {
      var s = el._nod;
      play(el, function () {
        if (!on(el.dataset.loop)) return;
        clearTimeout(s.timer);
        s.timer = setTimeout(function () {
          if (s.visible) cycle(el);
          else s.waiting = true;
        }, +el.dataset.every || DEFAULTS.every);
      });
    }

    function init() {
      var io = new IntersectionObserver(function (entries) {
        entries.forEach(function (e) {
          var s = e.target._nod;
          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-nod]').forEach(function (el) {
        if (el._nod) return;
        el._nod = {};
        if (on(el.dataset.hover)) {
          el.addEventListener('pointerenter', function () { play(el); });
          el.addEventListener('pointerdown', function () { play(el); });
        }
        if (!reduced()) io.observe(el);
      });
    }

    window.animacious = window.animacious || {};
    window.animacious.nod = function (el) { if (el._nod) play(el); };
    if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', init);
    else init();
  })();
</script>
2 · Your HTML
<div class="avatar" data-nod>…</div>

<!-- or three nods, repeating: -->
<div class="avatar" data-nod data-times="3" data-loop>…</div>