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.
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



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-inputis 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-visibleadds a 2 px outline (--wlk-ringcolour) 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-knobwithout legs. CSS transition is set to zero. The state remains readable with no character movement. - Forced colours (Windows):
forced-colors: activereplaces all variables with system colours (Highlight,Canvas,CanvasText).forced-color-adjust: noneprotects the SVG character to preserve its shapes. - Real form field:
.wlk-inputcarries anameattribute and is submitted inside a<form>like any checkbox. Anaria-live="polite"attribute on an adjacent.wlk-summaryannounces 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.
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):
<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 / property | Default | Effect |
|---|---|---|
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
<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.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..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.