AI CSS Animations

Magnet Loader

A horseshoe magnet pulls a stream of dots onto its poles, each one speeding up as it gets closer and vanishing on contact, while dashed field lines pulse between the poles.

Also known as: magnet animation, magnetic attraction loader, horseshoe magnet loader.

HTML

<div class="ldr-magnet" role="status" aria-label="Loading">
  <span class="ldr-magnet-field"></span>
  <span class="ldr-magnet-horseshoe"></span>
  <span class="ldr-magnet-dot" style="--i: 0; --lane: 0; --dy: -0.12"></span>
  <span class="ldr-magnet-dot" style="--i: 1; --lane: 1; --dy: 0.1"></span>
  <span class="ldr-magnet-dot" style="--i: 2; --lane: 0; --dy: 0.08"></span>
  <span class="ldr-magnet-dot" style="--i: 3; --lane: 1; --dy: -0.06"></span>
  <span class="ldr-magnet-dot" style="--i: 4; --lane: 0; --dy: -0.02"></span>
  <span class="ldr-magnet-dot" style="--i: 5; --lane: 1; --dy: 0.13"></span>
</div>

CSS

.ldr-magnet {
  --s: var(--ldr-size, 48px);
  --c: var(--ldr-color, #3b82f6);
  position: relative;
  width: calc(var(--s) * 1.6);
  height: var(--s);
}
.ldr-magnet-field,
.ldr-magnet-field::after {
  position: absolute;
  top: calc(var(--s) * 0.235);
  left: calc(var(--s) * 0.5);
  width: calc(var(--s) * 0.34);
  height: calc(var(--s) * 0.53);
  box-sizing: border-box;
  border: 1px dashed color-mix(in srgb, var(--ldr-color, #3b82f6) 55%, transparent);
  border-left: 0;
  border-radius: 0 50% 50% 0 / 0 50% 50% 0;
  animation: ldr-magnet-field 1.8s ease-in-out infinite;
}
.ldr-magnet-field::after {
  content: "";
  top: -1px;
  left: 0;
  width: calc(var(--s) * 0.7);
  height: calc(100% + 2px);
  animation-delay: -0.9s;
}
.ldr-magnet-horseshoe {
  position: absolute;
  top: calc(var(--s) * 0.15);
  left: 0;
  width: calc(var(--s) * 0.56);
  height: calc(var(--s) * 0.7);
  box-sizing: border-box;
  border: calc(var(--s) * 0.17) solid var(--c);
  border-right: 0;
  border-radius: calc(var(--s) * 0.35) 0 0 calc(var(--s) * 0.35);
}
.ldr-magnet-horseshoe::before,
.ldr-magnet-horseshoe::after {
  content: "";
  position: absolute;
  top: calc(var(--s) * -0.17);
  right: 0;
  width: calc(var(--s) * 0.13);
  height: calc(var(--s) * 0.17);
  background: color-mix(in srgb, var(--c) 40%, #fff);
}
.ldr-magnet-horseshoe::after {
  top: auto;
  bottom: calc(var(--s) * -0.17);
}
.ldr-magnet-dot {
  position: absolute;
  top: calc(var(--s) * (0.235 + var(--lane) * 0.53));
  left: calc(var(--s) * 1.45);
  width: calc(var(--s) / 9);
  height: calc(var(--s) / 9);
  margin: calc(var(--s) / -18);
  border-radius: 50%;
  background: var(--c);
  opacity: 0;
  animation: ldr-magnet-pull 1.8s infinite;
  animation-delay: calc(var(--i) * -0.3s);
}
@keyframes ldr-magnet-pull {
  0%   { transform: translate(0, calc(var(--s) * var(--dy))) scale(0.6); opacity: 0; animation-timing-function: cubic-bezier(0.55, 0, 1, 0.45); }
  20%  { opacity: 1; }
  80%  { transform: translate(calc(var(--s) * -0.83), 0) scale(1); opacity: 1; }
  100% { transform: translate(calc(var(--s) * -0.83), 0) scale(1); opacity: 0; }
}
@keyframes ldr-magnet-field {
  0%, 100% { opacity: 0.35; }
  50%      { opacity: 1; }
}

@media (prefers-reduced-motion: reduce) {
  .ldr-magnet, .ldr-magnet *, .ldr-magnet::before, .ldr-magnet::after, .ldr-magnet *::before, .ldr-magnet *::after {
    animation-duration: 3s;
  }
}

Customizing this loader

  • Color: set --ldr-color on the element or a parent. Currently previewing #3b82f6.
  • Size: set --ldr-size. Every dimension in the snippet is derived from it. Currently previewing 64px.
  • Speed: change the duration on the animation shorthand. Delays on the children are relative, so they keep their rhythm.
  • Accessibility: the root carries role="status" and an aria-label so screen readers announce the wait. The reduced-motion rule at the end slows the animation for users who asked for less motion.

Similar loaders