FormsPremium

Stroll Switch

An animated character serves as the knob: it walks across the track on click, reverses mid-walk when toggled again, and falls asleep when the switch is off.

SVG + JSToggleCharacterAccessible

Updated

This effect is part of Effect.Labs — 846 vanilla effects, some free, some premium. Forms has 21 effects, including 2 free. Explore the category →

3 usage examples

Preference panel — mobile wellness app — Stroll Switch example 1

① Preference panel — mobile wellness app

WhenA Settings screen in a mobile wellness or health app: three to four preferences in a vertical list, each toggle paired with a label and a short description.
WhyThe character gives each setting a tangible presence a flat toggle cannot — the user clearly sees something changed, without intrusive animation or alert messages.
Settings--wlk-size: 32px, distinct --wlk-accent per row (violet, teal, orange, blue) to distinguish categories. Use .wlk-group on the container to enable the data-wlk-count counter in the header.
Product hero — connected device power toggle — Stroll Switch example 2

② Product hero — connected device power toggle

WhenMain section of a marketing page: the toggle in .wlk-stack mode is centred in the hero, large format, with data-wlk-auto running an automatic demo. The checkbox state drives a visual element in the scene (light on/off, brightening background).
WhyA large animated toggle with auto-demo immediately communicates the product concept without text or video; the walking character gives the product a memorable personality.
Settings--wlk-size: 76px, class .wlk-stack (label below the track), --wlk-accent: #22c55e, attribute data-wlk-auto. Listen to wlk:change to update scene visuals in response to the checkbox state.
Rules configuration — strategy game scenario editor — Stroll Switch example 3

③ Rules configuration — strategy game scenario editor

WhenA compact grid of rules to enable or disable in a strategy game editor: eight to twelve small toggles in .wlk-stack mode, spread across columns by category.
WhyAt small sizes the character remains readable thanks to the scalable SVG; the dense grid of tiny animated toggles makes each click tangible in an otherwise cold and technical interface.
Settings--wlk-size: 28px, class .wlk-stack, --wlk-speed: 1.5 (faster walk at small size). Colour --wlk-accent by rule category (red for offensive rules, blue for tactical rules).

How it works

The foundation is a real <input type="checkbox" role="switch"> inside a <label class="wlk-root">. It is invisible (opacity: 0) but covers the entire track, receiving every click, keyboard event and screen-reader announcement. Without JavaScript, .wlk-knob (a plain CSS button) takes over and slides via transition: transform — the toggle stays functional in all cases.

JavaScript creates an SVG (viewBox 0 0 190 100) inside the track. The character has a spherical body and two legs with three segments each (hip → knee → ankle → toe). The physics does not play a frame-by-frame animation: a damped spring (spring()) drives the body position and squash (sq), while each foot stays planted as long as it bears the weight — the heel rises, the toe stays — then takes a step calculated from its position, speed and the body's inertia. Knees bend automatically from geometry (two equal-length segments). The requestAnimationFrame loop stops as soon as nothing is moving and restarts on each interaction.

Four phases manage state: seated (0), crouching (1), standing and walking (2), sitting back down (3). A click mid-walk reverses dir: the body brakes, feet adapt, and the character goes back without jumping. The off state (phase 0) leaves the character asleep — eyes closed, slight low smile.

The CSS variable --wlk-p (0 → 1) is written to .wlk-root on each frame as the character moves. It drives the track fill mask (mask-image: linear-gradient), the glow below the track (filter: blur on ::before), and the checkmark opacity. An IntersectionObserver pauses the loop when the toggle leaves the viewport.

Accessibility

  • Native checkbox: .wlk-input is an <input type="checkbox" role="switch"> inside a <label>. Screen readers announce the field name, state (on/off) and role with no extra ARIA. Tab focuses it, Space toggles; Enter is also caught by the script.
  • Visible focus: :focus-visible adds a 2 px outline (--wlk-ring colour) to the track. In high-contrast mode (.wlk-hc), the outline grows to 3 px and the decorative glow is removed.
  • Reduced motion: under prefers-reduced-motion: reduce (or class .wlk-calm), the script slides the CSS .wlk-knob without legs. CSS transition is set to zero. The state remains readable with no character movement.
  • Forced colours (Windows): forced-colors: active replaces all variables with system colours (Highlight, Canvas, CanvasText). forced-color-adjust: none protects the SVG character to preserve its shapes.
  • Real form field: .wlk-input carries a name attribute and is submitted inside a <form> like any checkbox. An aria-live="polite" attribute on an adjacent .wlk-summary announces the new state without interrupting speech.

Browser compatibility

DOM, CSS and JS-animated SVG, no dependency. CSS custom properties available since Chrome 49, Firefox 31, Safari 9.1. IntersectionObserver since Chrome 51, Firefox 55, Safari 12.1. mask-image with -webkit- prefix since Chrome 15, Safari 7; an opacity: var(--wlk-p) fallback covers browsers without support via @supports not. Not verified on real Safari; expected full.

Chrome 51+✓ Full (measured)
Firefox 55+✓ Full (measured)
Safari 12.1+✓ Expected full (not measured)
Edge 79+✓ Full (Chromium engine)
Mobile iOS 12.2+✓ Full (not measured)
Android Chrome 51+✓ Full (Chromium engine)

Without JavaScript, .wlk-knob (CSS button) slides via transition: transform — the toggle stays usable. Without IntersectionObserver, the loop also runs off screen. Without mask-image support, the track lights up all at once (opacity) rather than filling left to right.

The code

HTML structure to paste into your page (the CSS + JS come with your purchase):

index.html — structure
<div class="fx-0888-root">
  <label class="wlk-root">
    <span class="wlk-text">
      <span class="wlk-label">…</span>
      <span class="wlk-state"></span>
    </span>
    <span class="wlk-switch">
      <input class="wlk-input" type="checkbox" role="switch" name="…">
      <span class="wlk-track" aria-hidden="true">
        <span class="wlk-fill"></span>
        <span class="wlk-knob"></span>
      </span>
    </span>
  </label>
</div>

One-time payment, no subscription — the code is yours forever

Customize

Options passed to the API or data-* attributes:

Option / propertyDefaultEffect
Size — CSS variable --wlk-size 36px The entire geometry follows: track, character, text, glow. Tested from 28 px (compact, dense list) to 110 px (full-screen hero). Below 28 px the legs become hard to read.
On colour — CSS variable --wlk-accent #6c5ce7 Colours the lit track, the glow and the checkmark. Use a colour with strong contrast against the track background (--wlk-track) to meet WCAG AA (3:1 minimum for UI components).
Walk speed — CSS variable --wlk-speed 1 0.5 = very slow, relaxed gait; 2 = brisk walk. The value is read at each loop start: change it live with StrollSwitch.get(el).refresh().
Off track — CSS variables --wlk-track and --wlk-edge #1a1b25 / #363849 Track background and border. On a light background, prefer the .wlk-light theme (pale grey track, white body, dark legs) rather than overriding these two variables separately.
Character body — CSS variables --wlk-body and --wlk-body-shade #f6f4ef / #bfbccf Top and shadow colours of the radial gradient that forms the body. For a dark character on a light background, set --wlk-body: #2a2b38 and --wlk-body-shade: #17182a.
Light theme — class .wlk-light on the label or a parent absent (dark theme) Switches the track to light grey, the body to white, and the legs to dark. Can coexist with a custom --wlk-accent.
Stack layout — class .wlk-stack on the label absent (label on the right) Places the label below the track, centre-aligned. Ideal for product heroes or icon-toggle grids.
Auto demo — attribute data-wlk-auto on the label absent Toggles automatically every 5 seconds when no one is interacting. Remove in production to avoid interfering with the real form.
JavaScript API — StrollSwitch.get(el) — Returns { set(bool), toggle(), refresh() }. refresh() re-reads the checkbox and CSS variables if you changed state or theme programmatically. StrollSwitch.init(scope) initialises new toggles added dynamically.

FAQ

Yes. Remove the effect's <script>: .wlk-knob takes over — a plain round button that slides via CSS. The track, accent colour and glow remain; only the animated character disappears.
Use StrollSwitch.get(el).set(true) or .toggle(). These methods update the checkbox and trigger the animation without firing a native change event, avoiding double calls in frameworks that already listen to it. To catch both user clicks and programmatic changes, listen to wlk:change instead — its detail.checked is always up to date.
Yes. Add class .wlk-group to the shared container and attribute data-wlk-count to any text element inside — for example <span data-wlk-count>On: {n} of {t}</span>. {n} is the number of checked boxes, {t} the total; the text updates on every toggle. The attribute placed directly on a label (without .wlk-group) also works standalone.