← Entrances

Light Speed In

The element streaks in from off screen, leaning back with the speed, then brakes hard: it tips forward past upright and rocks back straight.

Fast deliveryHere in 15 minutes

How to use it

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

Options

Add any of these attributes to the same element.

data-fromThe side it comes from: right or left. Give both, right left, and each loop uses the next. Default right.
data-skewHow far it leans at full speed, in degrees. Default 30.
data-distanceTravel this many px instead of starting fully off screen.
data-durationHow long the entrance takes. Default 900 ms.
data-delayWait before the first entrance. Default 0 ms.
data-loopHold, fade away and streak in again.
data-holdHow long it stays before fading, when looping. Default 1400 ms.

It starts just past the edge of the nearest container that clips its overflow (overflow: hidden or clip), or of the window if there isn’t one. Put it in a clipping wrapper, or give the page overflow-x: clip, so it can’t make the page scroll sideways while it’s off screen.

1 · Snippet
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/bundles/anime.umd.min.js"></script>
<script>
  (function () {
    // Light Speed In: the element streaks in from off screen, skewed by its
    // speed, then brakes hard: it leans forward past upright and straightens.
    // Usage: <div data-light-speed-in>…</div>
    // It starts just past the edge of the nearest container that clips its
    // overflow (overflow: hidden or clip), or of the window.
    // Optional: data-from ("right", default, or "left"; give both, "right
    // left", and each loop uses the next), data-skew (degrees of lean at full
    // speed, default 30), data-distance (px to travel instead of from off
    // screen), data-duration (ms, default 900), data-delay (ms before the
    // first entrance), data-loop (hold, fade away and streak in again),
    // data-hold (ms to stay when looping, default 1400).
    var DEFAULTS = { from: 'right', skew: 30, duration: 900, delay: 0, hold: 1400 };

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

    // The box the element must leave to be "off screen".
    function clipBox(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();
      }
      return { left: 0, top: 0, right: window.innerWidth, bottom: window.innerHeight };
    }

    function enter(el, from, done) {
      var d = el.dataset;
      var t = +d.duration || DEFAULTS.duration;
      var skew = d.skew === undefined ? DEFAULTS.skew : +d.skew || 0;
      var r = el.getBoundingClientRect();
      var c = clipBox(el);
      // +1 when it comes from the right (moving left), -1 from the left.
      var dir = from === 'left' ? -1 : 1;
      var dist = +d.distance || (dir > 0 ? c.right - r.left : r.right - c.left) + 8;
      // At speed its top trails behind.
      anime.utils.set(el, { x: dir * dist, skewX: -dir * skew, opacity: 0 });
      anime.animate(el, {
        x: { to: 0, duration: t * 0.6, ease: 'out(3)' },
        skewX: [
          // Braking throws the top forward, then it rocks back upright.
          { to: dir * skew * 0.66, duration: t * 0.6, ease: 'out(2)' },
          { to: -dir * skew * 0.17, duration: t * 0.2, ease: 'inOut(2)' },
          { to: 0, duration: t * 0.2, ease: 'out(2)' }
        ],
        opacity: { to: 1, duration: t * 0.5, ease: 'out(2)' },
        onComplete: done
      });
    }

    function cycle(el) {
      var d = el.dataset;
      var s = el._lsi;
      var froms = (d.from || DEFAULTS.from).split(/[\s,]+/);
      enter(el, froms[s.n++ % froms.length], 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._lsi;
          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-light-speed-in]').forEach(function (el) {
        if (el._lsi) return;
        el._lsi = { 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="card" data-light-speed-in>Fast delivery</div>

<!-- or from the left, leaning harder: -->
<div class="card" data-light-speed-in data-from="left" data-skew="40">Fast delivery</div>