Experiments

Card Shimmer

A comet of light that sweeps around a card border once, built from a masked, rotating conic-gradient.

cssanimation

Card shimmer

Hover, re-trigger on every scroll, and the rainbow variant

Hover me

data-shimmer="hover"

Every time

data-shimmer="always" — scroll away and back.

Rainbow

.shimmer--rainbow (hover)

Use it on a site

Copy the CSS and JS into the project, then add data-shimmer to any element

HTML index.html
<link rel="stylesheet" href="shimmer-border.css" />

<!-- Any element with a border-radius works -->
<div class="card" data-shimmer>Plays once when scrolled into view</div>
<div class="card" data-shimmer="always">Plays every time it scrolls into view</div>
<div class="card" data-shimmer="hover" tabindex="0">Plays on hover or focus</div>
<div class="card shimmer--rainbow" data-shimmer="hover" tabindex="0">Rainbow</div>

<script type="module">
  import { initShimmer } from './shimmer-border.js';
  initShimmer();
</script>
CSS shimmer-border.css
/*
 * Shimmer border: a comet of light that sweeps once around an element's border.
 * Works on any element with a border-radius. Pair with shimmer-border.js, or add
 * the .is-shimmering class yourself to play it.
 */
@property --shimmer-angle {
  syntax: '<angle>';
  inherits: false;
  initial-value: 0deg;
}
/* Scales the tail: short as the comet sets off, full length mid-sweep, short again at the end. */
@property --shimmer-stretch {
  syntax: '<number>';
  inherits: false;
  initial-value: 1;
}
.shimmer {
  --shimmer-color: rgb(255 255 255 / 0.95);
  --shimmer-width: 1px;
  --shimmer-length: 90deg;
  --shimmer-start: -60deg;
  --shimmer-duration: 1.8s;
  --shimmer-ease: cubic-bezier(0.45, 0, 0.25, 1);
  position: relative;
  isolation: isolate;
}
.shimmer::before {
  content: '';
  position: absolute;
  inset: calc(var(--shimmer-width) * -1);
  z-index: 1;
  padding: var(--shimmer-width);
  border-radius: inherit;
  pointer-events: none;
  opacity: 0;
  background: conic-gradient(
    from calc(var(--shimmer-start) + var(--shimmer-angle)),
    transparent 0deg,
    transparent calc(360deg - var(--shimmer-length) * var(--shimmer-stretch)),
    var(--shimmer-color) 360deg
  );
  -webkit-mask:
    linear-gradient(#000 0 0) content-box,
    linear-gradient(#000 0 0);
  -webkit-mask-composite: xor;
  mask-composite: exclude;
}
.shimmer.is-shimmering::before {
  animation: shimmer-sweep var(--shimmer-duration) var(--shimmer-ease) var(--shimmer-delay, 0ms)
    both;
}
@keyframes shimmer-sweep {
  0% {
    --shimmer-angle: 0deg;
    --shimmer-stretch: 0.2;
    opacity: 0;
  }
  12% {
    opacity: 1;
  }
  50% {
    --shimmer-stretch: 1;
  }
  80% {
    opacity: 1;
  }
  100% {
    --shimmer-angle: 360deg;
    --shimmer-stretch: 0.2;
    opacity: 0;
  }
}
/* Same length and linear fade-in as the white tail, with the colours ramping up in opacity. */
.shimmer--rainbow::before {
  background: conic-gradient(
    from calc(var(--shimmer-start) + var(--shimmer-angle)),
    transparent 0deg,
    transparent calc(360deg - var(--shimmer-length) * var(--shimmer-stretch)),
    rgb(255 95 109 / 0.2) calc(360deg - var(--shimmer-length) * var(--shimmer-stretch) * 0.8),
    rgb(255 195 113 / 0.4) calc(360deg - var(--shimmer-length) * var(--shimmer-stretch) * 0.6),
    rgb(71 232 145 / 0.6) calc(360deg - var(--shimmer-length) * var(--shimmer-stretch) * 0.4),
    rgb(58 184 255 / 0.8) calc(360deg - var(--shimmer-length) * var(--shimmer-stretch) * 0.2),
    rgb(176 107 255 / 0.95) 360deg
  );
}
@media (prefers-reduced-motion: reduce) {
  .shimmer.is-shimmering::before {
    animation: none;
  }
}
JS shimmer-border.js
/*
 * Shimmer border: plays the effect from shimmer-border.css.
 * Add data-shimmer to an element, then call initShimmer(). Modes:
 *   data-shimmer          plays once when scrolled into view (default)
 *   data-shimmer="always" plays every time it scrolls into view
 *   data-shimmer="hover"  plays on hover or keyboard focus
 *   data-shimmer="manual" only plays when you call shimmer(el)
 */
const DEFAULTS = {
  selector: '[data-shimmer]',
  threshold: 0.5,
  rootMargin: '0px 0px -10% 0px',
  stagger: 120, // ms between elements that enter the viewport together
};
const reducedMotion = matchMedia('(prefers-reduced-motion: reduce)');
const bound = new WeakSet();

/** Play the shimmer on one element, restarting it if it is already running. */
export function shimmer(el, delay = 0) {
  if (reducedMotion.matches) return;
  el.style.setProperty('--shimmer-delay', `${delay}ms`);
  el.classList.remove('is-shimmering');
  void el.offsetWidth;
  el.classList.add('is-shimmering');
}

function onAnimationEnd(e) {
  if (e.animationName === 'shimmer-sweep') e.currentTarget.classList.remove('is-shimmering');
}

function onHover(e) {
  if (!e.currentTarget.classList.contains('is-shimmering')) shimmer(e.currentTarget);
}

/** Wire up every [data-shimmer] element under root. Safe to call again for new content. */
export function initShimmer(options = {}, root = document) {
  const opts = { ...DEFAULTS, ...options };
  const els = [...root.querySelectorAll(opts.selector)].filter((el) => !bound.has(el));
  const io = new IntersectionObserver(
    (entries) => {
      let i = 0;
      for (const entry of entries) {
        if (!entry.isIntersecting) continue;
        shimmer(entry.target, i++ * opts.stagger);
        if ((entry.target.dataset.shimmer || 'once') === 'once') io.unobserve(entry.target);
      }
    },
    { threshold: opts.threshold, rootMargin: opts.rootMargin },
  );
  for (const el of els) {
    bound.add(el);
    el.classList.add('shimmer');
    el.addEventListener('animationend', onAnimationEnd);
    const mode = el.dataset.shimmer || 'once';
    if (mode === 'hover') {
      el.addEventListener('pointerenter', onHover);
      el.addEventListener('focusin', onHover);
    } else if (mode !== 'manual') {
      io.observe(el);
    }
  }
}