AI CSS Animations

Toggle Switch Loading State

A toggle that slides on, spins a small ring inside its knob while the change saves, then fills with color once it lands, and later flips back. For settings that need a server round trip.

Also known as: switch with spinner, toggle loading spinner, async toggle switch.

HTML

<button class="ldr-loading-toggle-switch" type="button" role="switch" aria-checked="true" aria-busy="true" aria-label="Sync">
  <span class="ldr-loading-toggle-switch-knob" aria-hidden="true"></span>
</button>

CSS

.ldr-loading-toggle-switch {
  --s: var(--ldr-size, 48px);
  --c: var(--ldr-color, #3b82f6);
  --pad: calc(var(--s) / 12);
  --knob: calc(var(--s) * 0.9 - var(--pad) * 2);
  position: relative;
  width: calc(var(--s) * 1.6);
  height: calc(var(--s) * 0.9);
  padding: 0;
  border: 0;
  border-radius: calc(var(--s) * 0.45);
  background: color-mix(in srgb, var(--ldr-color, #3b82f6) 28%, transparent);
  cursor: progress;
}
.ldr-loading-toggle-switch::before {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background: var(--c);
  opacity: 0;
  animation: ldr-loading-toggle-switch-fill 3.6s cubic-bezier(0.45, 0, 0.55, 1) infinite;
}
.ldr-loading-toggle-switch-knob {
  position: absolute;
  left: var(--pad);
  top: var(--pad);
  width: var(--knob);
  height: var(--knob);
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 2px rgb(0 0 0 / 0.25);
  animation: ldr-loading-toggle-switch-slide 3.6s cubic-bezier(0.5, 0, 0.2, 1.2) infinite;
}
.ldr-loading-toggle-switch-knob::after {
  content: "";
  position: absolute;
  inset: 22%;
  box-sizing: border-box;
  border: max(1px, calc(var(--s) / 24)) solid color-mix(in srgb, var(--ldr-color, #3b82f6) 25%, transparent);
  border-top-color: var(--c);
  border-radius: 50%;
  animation:
    ldr-loading-toggle-switch-spin 0.7s linear infinite,
    ldr-loading-toggle-switch-busy 3.6s ease infinite;
}
@keyframes ldr-loading-toggle-switch-slide {
  0%, 8%   { transform: translateX(0); }
  20%, 66% { transform: translateX(calc(var(--s) * 0.7)); }
  78%, 100% { transform: translateX(0); }
}
@keyframes ldr-loading-toggle-switch-busy {
  0%, 14%  { opacity: 0; scale: 0.4; }
  20%, 44% { opacity: 1; scale: 1; }
  50%, 100% { opacity: 0; scale: 0.4; }
}
@keyframes ldr-loading-toggle-switch-fill {
  0%, 44%  { opacity: 0; }
  52%, 68% { opacity: 1; }
  76%, 100% { opacity: 0; }
}
@keyframes ldr-loading-toggle-switch-spin {
  to { transform: rotate(360deg); }
}

@media (prefers-reduced-motion: reduce) {
  .ldr-loading-toggle-switch, .ldr-loading-toggle-switch *, .ldr-loading-toggle-switch::before, .ldr-loading-toggle-switch::after, .ldr-loading-toggle-switch *::before, .ldr-loading-toggle-switch *::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