Skip to content

176 web animation techniques, each a live demo with plain settings and a ready-made prompt for your AI assistant. No dependencies, no build step.

feat: move Toggle / Switch Slide to the guided-steps page

matin580efdd8902f
diff --git a/animations/04-micro-interactions/toggle-switch/README.md b/animations/04-micro-interactions/toggle-switch/README.md
index bcd60dbb..34ff94fa 100644
--- a/animations/04-micro-interactions/toggle-switch/README.md
+++ b/animations/04-micro-interactions/toggle-switch/README.md
@@ -1,7 +1,7 @@
# Toggle / Switch Slide
## What it is
-A toggle switch is a binary control — on or off — where the thumb pill slides horizontally between states. Unlike a checkbox, which changes state instantly, a toggle communicates the transition between states through motion. The animation duration and easing determine whether the control feels mechanical, weighted, or springy.
+A toggle switch is an on/off control whose round knob slides across a track, and the track fills with color when it is on. The slide shows the change instead of just flipping it, and its speed and curve decide whether the switch feels mechanical, weighty or springy.
## When to use it
- Settings and preferences panels (notifications on/off, dark mode, feature flags)
@@ -51,10 +51,10 @@ For an elastic feel, swap the thumb transition easing to `cubic-bezier(.34, 1.56
## Key parameters
| Parameter | Default | Effect |
|-----------|---------|--------|
-| Duration | 200ms | Under 120ms = mechanical; over 350ms = sluggish |
-| Easing | Material smooth | `cubic-bezier(.4,0,.2,1)` — weighted deceleration |
-| Thumb travel | 22px | (track width - 2×padding - thumb width) |
-| On color | accent blue | Must contrast 3:1 with the thumb color (white) |
+| Speed | Normal | How long the knob takes to slide: slow is 320ms, normal 200ms and fast 120ms; under 120ms feels mechanical, over 350ms sluggish |
+| Feel | Gentle | Gentle eases in and out, which gives the knob weight; Smooth starts fast and slows to a stop; Even slides at one steady speed |
+| Springy knob | on | The third switch's knob goes a little past the end, then settles |
+| On color | Blue | The track color when a switch is on; it must stand out from the white knob. The Appearance switch keeps its own orange |
## Production notes
- **Accessibility**: the hidden `<input type="checkbox">` provides keyboard control, `aria-checked` state, and screen reader announcements for free — no JS needed for state management.
@@ -65,6 +65,6 @@ For an elastic feel, swap the thumb transition easing to `cubic-bezier(.34, 1.56
- **Framer Motion**: animate the `x` value of a `<motion.div>` between 0 and the track width — simpler than CSS for controlled components.
## See also
-- [Button Press Scale](../button-press-scale/) — press-down feedback on toggle tap
-- [Form Field Morph](../form-field-morph/) — another input state transition pattern
-- [Accordion Open/Close](../accordion/) — another binary expand/collapse pattern
+- [Button Press Scale](../button-press-scale/) — the button shrinks while it is pressed
+- [Form Field Morph](../form-field-morph/) — the label moves up when a field is focused
+- [Accordion Open/Close](../accordion/) — a panel opens and closes smoothly
diff --git a/animations/04-micro-interactions/toggle-switch/index.html b/animations/04-micro-interactions/toggle-switch/index.html
index 5be9883e..9dd62ea6 100644
--- a/animations/04-micro-interactions/toggle-switch/index.html
+++ b/animations/04-micro-interactions/toggle-switch/index.html
@@ -4,7 +4,7 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Toggle / Switch Slide — Animation Handbook</title>
- <meta name="description" content="Pill slides left/right for on/off — smooth cubic-bezier gives a weighted, physical feel.">
+ <meta name="description" content="The knob slides across as the switch turns on or off. Best for settings.">
<link rel="canonical" href="https://matinmonshizadeh.github.io/animation-handbook/animations/04-micro-interactions/toggle-switch/">
<link rel="icon" type="image/png" sizes="96x96" href="../../../favicon.png">
<link rel="icon" type="image/svg+xml" href="../../../favicon.svg">
@@ -12,41 +12,33 @@
<meta property="og:type" content="article">
<meta property="og:site_name" content="Animation Handbook">
<meta property="og:title" content="Toggle / Switch Slide — Animation Handbook">
- <meta property="og:description" content="Pill slides left/right for on/off — smooth cubic-bezier gives a weighted, physical feel.">
+ <meta property="og:description" content="The knob slides across as the switch turns on or off. Best for settings.">
<meta property="og:url" content="https://matinmonshizadeh.github.io/animation-handbook/animations/04-micro-interactions/toggle-switch/">
<meta property="og:image" content="https://matinmonshizadeh.github.io/animation-handbook/og-image.png">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:title" content="Toggle / Switch Slide — Animation Handbook">
- <meta name="twitter:description" content="Pill slides left/right for on/off — smooth cubic-bezier gives a weighted, physical feel.">
+ <meta name="twitter:description" content="The knob slides across as the switch turns on or off. Best for settings.">
<meta name="twitter:image" content="https://matinmonshizadeh.github.io/animation-handbook/og-image.png">
- <script type="application/ld+json">{"@context": "https://schema.org", "@type": "TechArticle", "headline": "Toggle / Switch Slide", "name": "Toggle / Switch Slide", "description": "Pill slides left/right for on/off — smooth cubic-bezier gives a weighted, physical feel.", "url": "https://matinmonshizadeh.github.io/animation-handbook/animations/04-micro-interactions/toggle-switch/", "inLanguage": "en", "articleSection": "Micro-Interactions", "isPartOf": {"@type": "WebSite", "name": "Animation Handbook", "url": "https://matinmonshizadeh.github.io/animation-handbook/"}, "author": {"@type": "Person", "name": "matinmonshizadeh"}, "keywords": "web animation, CSS animation, Micro-Interactions, Toggle / Switch Slide"}</script>
+ <script type="application/ld+json">{"@context": "https://schema.org", "@type": "TechArticle", "headline": "Toggle / Switch Slide", "name": "Toggle / Switch Slide", "description": "The knob slides across as the switch turns on or off. Best for settings.", "url": "https://matinmonshizadeh.github.io/animation-handbook/animations/04-micro-interactions/toggle-switch/", "inLanguage": "en", "articleSection": "Micro-Interactions", "isPartOf": {"@type": "WebSite", "name": "Animation Handbook", "url": "https://matinmonshizadeh.github.io/animation-handbook/"}, "author": {"@type": "Person", "name": "matinmonshizadeh"}, "keywords": "web animation, CSS animation, Micro-Interactions, Toggle / Switch Slide"}</script>
<style>
- @font-face{font-family:'Bricolage';src:url('../../../assets/fonts/brico-700.woff2') format('woff2');font-weight:700;font-display:swap}
- @font-face{font-family:'Bricolage';src:url('../../../assets/fonts/brico-800.woff2') format('woff2');font-weight:800;font-display:swap}
- @font-face{font-family:'PlexMono';src:url('../../../assets/fonts/plex-400.woff2') format('woff2');font-weight:400;font-display:swap}
- @font-face{font-family:'PlexMono';src:url('../../../assets/fonts/plex-500.woff2') format('woff2');font-weight:500;font-display:swap}
- :root{--bg:#0b0b0d;--ui-bg:#111114;--ui-border:rgba(255,255,255,.09);--ui-accent:#ff9d5c;--ui-text:#f4f4f2;--ui-muted:#77777e;--disp:'Bricolage','Arial Narrow',sans-serif;--mono:'PlexMono',ui-monospace,Menlo,Consolas,monospace;--stage-h:620px;--tog-dur:200ms;--tog-ease:cubic-bezier(.4,0,.2,1);--on-color:#58a6ff}
+ :root{--ui-accent:#ff9d5c;--ui-border:rgba(255,255,255,.09);--ui-text:#f4f4f2;--ui-muted:#8a8a92;--tog-dur:200ms;--tog-ease:cubic-bezier(.4,0,.2,1);--on-color:#58a6ff}
*,*::before,*::after{box-sizing:border-box;margin:0;padding:0}
- body{background:var(--bg);color:var(--ui-text);font-family:var(--mono);-webkit-font-smoothing:antialiased;padding:clamp(12px,3vw,24px)}
- header{margin-bottom:20px}
- header h1{font-family:var(--disp);font-weight:700;font-size:clamp(11px,1.8vw,15px);letter-spacing:-.01em}
- header p{font-size:clamp(10px,1.2vw,12px);color:var(--ui-muted);margin-top:4px}
- .layout{display:flex;gap:clamp(12px,2vw,24px);align-items:flex-start}
- .stage{flex:1;min-width:0;height:var(--stage-h);border:1px solid var(--ui-border);border-radius:8px;background:var(--bg);display:flex;flex-direction:column;align-items:center;justify-content:center;gap:36px;padding:32px}
- /* Toggle row */
+ .stage{display:flex;flex-direction:column;align-items:center;justify-content:center;gap:36px;padding:32px;background:#0b0b0d}
+ /* A window this short gives the stage 260px, less than the three switches need: the stage gives up some spacing, the switches keep their size */
+ @media(min-width:601px) and (max-height:620px){.stage{gap:20px;padding:20px}}
.tog-row{display:flex;align-items:center;gap:16px;width:100%;max-width:320px}
- .tog-label{flex:1;font-size:12px;color:var(--ui-text)}
- .tog-sub{font-size:10px;color:var(--ui-muted);display:block;margin-top:2px}
- /* Base track */
- /* Label is 44px tall for touch; the visible track is inset to 30px (36px for -lg) */
+ .tog-label{flex:1;font-size:15px;font-weight:600;color:var(--ui-text)}
+ /* Base track. The label is 44px tall for touch; the visible track is inset to 30px (36px for -lg) */
.sw{position:relative;width:52px;height:44px;flex-shrink:0;cursor:pointer;-webkit-user-select:none;user-select:none;touch-action:manipulation}
.sw input{position:absolute;opacity:0;width:0;height:0}
.sw-track{position:absolute;inset:7px 0;border-radius:15px;background:var(--ui-border);transition:background var(--tog-dur) var(--tog-ease)}
- .sw-thumb{position:absolute;top:10px;left:3px;width:24px;height:24px;border-radius:50%;background:#fff;transition:transform var(--tog-dur) var(--tog-ease),box-shadow var(--tog-dur) var(--tog-ease);box-shadow:0 1px 4px rgba(0,0,0,.4)}
+ .sw-thumb{position:absolute;top:10px;left:3px;width:24px;height:24px;border-radius:50%;background:#fff;transition:transform var(--tog-dur) var(--tog-ease);box-shadow:0 1px 4px rgba(0,0,0,.4)}
.sw input:checked~.sw-track{background:var(--on-color)}
.sw input:checked~.sw-thumb{transform:translateX(22px)}
- /* Elastic variant */
- .sw.elastic .sw-thumb{transition:transform var(--tog-dur) cubic-bezier(.34,1.56,.64,1),background var(--tog-dur) var(--tog-ease)}
+ /* The checkbox is invisible, so keyboard focus shows on the track beside it */
+ .sw input:focus-visible~.sw-track{outline:2px solid var(--ui-accent);outline-offset:2px}
+ /* Springy knob variant */
+ .sw.elastic .sw-thumb{transition:transform var(--tog-dur) cubic-bezier(.34,1.56,.64,1)}
/* Large with icons */
.sw-lg{width:68px}
.sw-lg .sw-track{inset:4px 0}
@@ -55,119 +47,194 @@
.sw-lg input:checked~.sw-thumb{transform:translateX(32px)}
.sw-lg input:checked~.sw-thumb::before{content:'☀️'}
.sw-lg input:checked~.sw-track{background:#ffa657}
- /* Status readout */
- .status-row{display:flex;gap:16px;font-size:10px;color:var(--ui-muted)}
- .sv{color:var(--ui-text)}
- aside{width:clamp(200px,22vw,240px);flex-shrink:0;background:var(--ui-bg);border:1px solid var(--ui-border);border-radius:8px;padding:16px;display:flex;flex-direction:column;gap:10px}
- .note{font-size:10px;color:var(--ui-muted);line-height:1.6;border-left:2px solid var(--ui-accent);padding-left:8px}
- .lbl{font-size:10px;text-transform:uppercase;letter-spacing:1px;color:var(--ui-accent)}
- .div{height:1px;background:var(--ui-border)}
- .sr{display:flex;align-items:center;gap:6px;font-size:10px}
- .sr input[type=range]{flex:1;accent-color:var(--ui-accent);min-height:44px}
- .sval{width:48px;text-align:right;color:var(--ui-text)}
- select{background:var(--ui-bg);border:1px solid var(--ui-border);color:var(--ui-text);font-family:monospace;font-size:10px;padding:8px 6px;border-radius:4px;width:100%;min-height:44px}
- .tog-inp{display:flex;align-items:center;gap:8px;font-size:10px;cursor:pointer;min-height:44px}
- .tog-inp input{accent-color:var(--ui-accent);width:16px;height:16px;flex-shrink:0}
- input[type=color]{border:none;background:none;width:44px;height:44px;cursor:pointer;border-radius:4px;padding:0}
- @media(max-width:600px){.layout{flex-direction:column}aside{width:100%}:root{--stage-h:auto}.stage{width:100%;height:auto;min-height:420px}}
@media(prefers-reduced-motion:reduce){.sw-track,.sw-thumb{transition:background .1s!important}}
</style>
-<style id="ah-inject">
-:root{--ah-bg:rgba(17,17,20,.82);--ah-line:rgba(255,255,255,.1);--ah-ink:#f4f4f2;--ah-muted:#9a9aa2;--ah-accent:#6ea8ff}
-body{padding-top:46px}
-.ah-bar{position:fixed;top:0;left:0;right:0;height:46px;z-index:2147483000;display:flex;align-items:center;justify-content:space-between;gap:10px;padding:0 clamp(10px,3vw,18px);background:var(--ah-bg);backdrop-filter:blur(10px);-webkit-backdrop-filter:blur(10px);border-bottom:1px solid var(--ah-line);font-family:ui-monospace,Menlo,Consolas,monospace;font-size:12px;box-sizing:border-box}
-.ah-bar a,.ah-bar button{color:var(--ah-muted);text-decoration:none;background:none;border:0;font:inherit;cursor:pointer;display:inline-flex;align-items:center;gap:6px;min-height:34px;padding:0 7px;border-radius:6px;transition:color .18s,background .18s;white-space:nowrap}
-.ah-bar a:hover,.ah-bar button:hover{color:var(--ah-ink)}
-.ah-home{color:var(--ah-ink);font-weight:600}
-.ah-actions{display:flex;align-items:center;gap:2px}
-.ah-sep{width:1px;height:16px;background:var(--ah-line);margin:0 4px}
-.ah-copied{color:var(--ah-accent)!important}
-@media(max-width:600px){.ah-bar .ah-hideSm{display:none}}
-@media print{.ah-bar{display:none}body{padding-top:0}}
-</style>
+ <link rel="stylesheet" href="../../../assets/css/demo-page.css?v=4">
+ <script src="../../../assets/js/demo-page.js?v=4" defer></script>
</head>
-<body>
-<div class="ah-bar" role="navigation" aria-label="Animation Handbook"><a class="ah-home" href="../../../">← Animation Handbook</a><span class="ah-actions"><a class="ah-hideSm" href="../magnetic-button/" rel="prev" title="Previous">‹ Prev</a><a class="ah-hideSm" href="../heart-burst/" rel="next" title="Next">Next ›</a><span class="ah-sep ah-hideSm"></span><button type="button" id="ah-copy" class="ah-hideSm">Copy source</button><a class="ah-hideSm" href="https://github.com/matinmonshizadeh/animation-handbook/blob/main/animations/04-micro-interactions/toggle-switch/index.html" target="_blank" rel="noopener noreferrer">Source ↗</a></span></div>
-<header>
- <h1>Toggle / Switch Slide</h1>
- <p>Pill slides left/right for on/off — smooth cubic-bezier gives a weighted, physical feel.</p>
-</header>
-<div class="layout">
- <div class="stage">
- <div class="tog-row">
- <div class="tog-label">Notifications<span class="tog-sub">Standard iOS-style toggle</span></div>
- <label class="sw" aria-label="Toggle notifications">
- <input type="checkbox" id="sw0" checked>
- <span class="sw-track"></span>
- <span class="sw-thumb"></span>
- </label>
+<body class="hb" data-hb-kind="do" data-hb-autoplay>
+<nav class="hb-bar" aria-label="Animation Handbook">
+ <a class="hb-home" href="../../../"><svg class="hb-mark" viewBox="0 0 32 32" aria-hidden="true"><g class="g"><circle cx="11.16" cy="20.84" r="6.1"/><circle cx="15.12" cy="16.88" r="6.1"/><circle cx="18.51" cy="13.49" r="6.1"/></g><circle class="b" cx="21.2" cy="10.8" r="6.6"/></svg><span>Animation Handbook</span></a>
+ <span class="hb-pager"><a href="../magnetic-button/" rel="prev" aria-label="Previous: Magnetic Button"><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="m15 18-6-6 6-6"/></svg><span><span class="hb-dir">Previous: </span>Magnetic Button</span></a><a href="../heart-burst/" rel="next" aria-label="Next: Heart / Like Burst"><span><span class="hb-dir">Next: </span>Heart / Like Burst</span><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="m9 18 6-6-6-6"/></svg></a></span>
+</nav>
+<main class="hb-page">
+ <header class="hb-head">
+ <p class="hb-cat">04.06 · Micro-Interactions</p>
+ <h1>Toggle / Switch Slide</h1>
+ <p class="hb-lede">The knob slides across as the switch turns on or off. Best for settings.</p>
+ </header>
+
+ <section class="hb-step hb-watch" aria-labelledby="hb-watch-title">
+ <div class="hb-step-head">
+ <span class="hb-num" aria-hidden="true">1</span>
+ <div><h2 id="hb-watch-title">Click it</h2><p>Click or tap a switch to turn it on or off, or press Show me.</p></div>
</div>
- <div class="tog-row">
- <div class="tog-label">Appearance<span class="tog-sub">Icon changes with state</span></div>
- <label class="sw sw-lg" aria-label="Toggle appearance">
- <input type="checkbox" id="sw1">
- <span class="sw-track"></span>
- <span class="sw-thumb"></span>
- </label>
+ <div class="stage hb-dots">
+ <div class="tog-row">
+ <div class="tog-label">Notifications</div>
+ <label class="sw" aria-label="Toggle notifications">
+ <input type="checkbox" id="sw0" checked autocomplete="off">
+ <span class="sw-track"></span>
+ <span class="sw-thumb"></span>
+ </label>
+ </div>
+ <div class="tog-row">
+ <div class="tog-label">Appearance</div>
+ <label class="sw sw-lg" aria-label="Toggle appearance">
+ <input type="checkbox" id="sw1" autocomplete="off">
+ <span class="sw-track"></span>
+ <span class="sw-thumb"></span>
+ </label>
+ </div>
+ <div class="tog-row">
+ <div class="tog-label">Auto-save</div>
+ <label class="sw elastic" aria-label="Toggle auto-save">
+ <input type="checkbox" id="sw2" checked autocomplete="off">
+ <span class="sw-track"></span>
+ <span class="sw-thumb"></span>
+ </label>
+ </div>
</div>
- <div class="tog-row">
- <div class="tog-label">Auto-save<span class="tog-sub">Elastic release overshoot</span></div>
- <label class="sw elastic" aria-label="Toggle auto-save">
- <input type="checkbox" id="sw2" checked>
- <span class="sw-track"></span>
- <span class="sw-thumb"></span>
- </label>
+ <div class="hb-player">
+ <button class="hb-play" type="button" id="btn-demo" data-hb-demo><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="M4.037 4.688a.495.495 0 0 1 .651-.651l16 6.5a.5.5 0 0 1-.063.947l-6.124 1.58a2 2 0 0 0-1.438 1.435l-1.579 6.126a.5.5 0 0 1-.947.063z"/></svg>Show me</button>
+ <label class="hb-toggle"><input class="hb-switch" type="checkbox" role="switch" id="slow-tog" data-hb-slowmo="css" autocomplete="off"><span>Slow motion</span></label>
</div>
- <div class="status-row">
- <span>Notif: <span class="sv" id="s0">On</span></span>
- <span>Appear: <span class="sv" id="s1">Off</span></span>
- <span>Save: <span class="sv" id="s2">On</span></span>
+ </section>
+
+ <section class="hb-step hb-try" aria-labelledby="hb-try-title">
+ <div class="hb-step-head">
+ <span class="hb-num" aria-hidden="true">2</span>
+ <div><h2 id="hb-try-title">Try it</h2><p>Change a setting, then try it again or press Show me.</p></div>
</div>
- </div>
- <aside>
- <p class="note">200ms with smooth easing reads as "physical" — the pill has mass. Under 120ms feels mechanical; over 350ms feels sluggish.</p>
- <div class="lbl">Duration</div>
- <div class="sr"><input type="range" id="dur-sl" min="120" max="400" value="200" step="10"><span class="sval" id="dur-v">200ms</span></div>
- <div class="lbl">Easing</div>
- <select id="ease-sel">
- <option value="cubic-bezier(.4,0,.2,1)" selected>Material smooth</option>
- <option value="ease-in-out">Ease in-out</option>
- <option value="ease-out">Ease out</option>
- <option value="linear">Linear</option>
- </select>
- <div class="lbl">On Color</div>
- <input type="color" id="color-pick" value="#58a6ff" aria-label="On color">
- <label class="tog-inp"><input type="checkbox" id="elastic-tog" checked><span>Elastic on toggle 3</span></label>
- <div class="div"></div>
- <p style="font-size:10px;color:var(--ui-muted)">Tap any toggle to flip state.</p>
- </aside>
-</div>
+ <div class="hb-settings">
+ <div class="hb-setting">
+ <p class="hb-setting-name" id="speed-lbl">Speed</p>
+ <div class="seg" id="speed-seg" role="group" aria-labelledby="speed-lbl">
+ <button type="button" data-ms="320" aria-pressed="false">Slow</button>
+ <button type="button" data-ms="200" class="on" aria-pressed="true">Normal</button>
+ <button type="button" data-ms="120" aria-pressed="false">Fast</button>
+ </div>
+ <p class="hb-hint">Fast feels mechanical; slow can feel sluggish.</p>
+ </div>
+ <div class="hb-setting">
+ <p class="hb-setting-name" id="feel-lbl">Feel</p>
+ <div class="seg" id="feel-seg" role="group" aria-labelledby="feel-lbl">
+ <button type="button" data-ease="ease-out" aria-pressed="false">Smooth</button>
+ <button type="button" data-ease="cubic-bezier(.4,0,.2,1)" class="on" aria-pressed="true">Gentle</button>
+ <button type="button" data-ease="linear" aria-pressed="false">Even</button>
+ </div>
+ <p class="hb-hint">Gentle gives the knob weight; Even feels robotic.</p>
+ </div>
+ </div>
+ <details class="hb-options">
+ <summary><span class="hb-more-label">More options</span><span class="hb-less">Fewer options</span><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="m6 9 6 6 6-6"/></svg></summary>
+ <div class="hb-settings">
+ <div class="hb-setting">
+ <label class="hb-switch-row"><span>Springy knob</span><input class="hb-switch" type="checkbox" role="switch" id="elastic-tog" checked autocomplete="off"></label>
+ <p class="hb-hint">The third switch's knob goes past the end, then settles.</p>
+ </div>
+ <div class="hb-setting">
+ <p class="hb-setting-name" id="color-lbl">On color</p>
+ <div class="swatches" id="color-sw" role="group" aria-labelledby="color-lbl">
+ <button type="button" aria-pressed="false" aria-label="Pink" data-c="#ff6f8b" style="background:#ff6f8b"></button>
+ <button type="button" class="on" aria-pressed="true" aria-label="Blue" data-c="#58a6ff" style="background:#58a6ff"></button>
+ <button type="button" aria-pressed="false" aria-label="Purple" data-c="#d2a8ff" style="background:#d2a8ff"></button>
+ <button type="button" aria-pressed="false" aria-label="Green" data-c="#56d364" style="background:#56d364"></button>
+ <button type="button" aria-pressed="false" aria-label="Orange" data-c="#ffa657" style="background:#ffa657"></button>
+ </div>
+ <p class="hb-hint">The track color when a switch is on.</p>
+ </div>
+ </div>
+ </details>
+ </section>
+
+ <section class="hb-step hb-prompt-step" aria-labelledby="hb-prompt-title">
+ <div class="hb-step-head">
+ <span class="hb-num" aria-hidden="true">3</span>
+ <div><h2 id="hb-prompt-title">Copy the prompt</h2><p>Paste it into an AI assistant such as ChatGPT, Claude or Cursor to build this in your own project.</p></div>
+ </div>
+ <div class="hb-card">
+ <p class="hb-prompt">Add an animated toggle switch to [the on/off setting you want to control]. Build it on a real checkbox so it works with the keyboard and screen readers. When it is turned on, the round knob slides across the track and the track fills with color; when it is turned off, both go back. When the settings ask for it, let the knob go slightly past the end and settle, so it feels springy. Make the whole switch and its label easy to tap. If the visitor has reduced motion turned on, move the knob without sliding. Match the settings listed below.</p>
+ <div class="hb-chips-box" hidden>
+ <p class="hb-chips-title">Your settings</p>
+ <ul class="hb-chips"></ul>
+ </div>
+ <div class="hb-copy-row">
+ <button class="hb-copy" type="button"><svg class="hb-ic hb-i-copy" viewBox="0 0 24 24" aria-hidden="true"><rect x="9" y="9" width="13" height="13" rx="2"/><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"/></svg><svg class="hb-ic hb-i-done" viewBox="0 0 24 24" aria-hidden="true"><path d="M20 6 9 17l-5-5"/></svg><span class="hb-copy-label" aria-live="polite">Copy prompt</span></button>
+ <p class="hb-hint">Replace the words in brackets with your own element. Your settings are added at the end.</p>
+ </div>
+ </div>
+ </section>
+
+ <section class="hb-about" aria-labelledby="hb-what-title">
+ <div class="hb-what">
+ <h2 id="hb-what-title">What it is</h2>
+ <div class="hb-what-text"><p>The full explanation is in <a href="README.md">README.md</a>.</p></div>
+ </div>
+ <div class="hb-fit">
+ <h2>Good for</h2>
+ <ul class="hb-tags hb-good"><li>Settings</li><li>Preferences</li><li>Dark mode switches</li></ul>
+ <h3>Avoid on</h3>
+ <ul class="hb-tags hb-avoid"><li>Forms sent later</li><li>More than two options</li></ul>
+ </div>
+ </section>
+
+ <section class="hb-related" aria-labelledby="hb-related-title" hidden>
+ <h2 id="hb-related-title">Similar animations</h2>
+ <div class="hb-rel-list"></div>
+ </section>
+</main>
+<footer class="hb-foot"><span>Animation Handbook</span><a href="#top">Back to top ↑</a></footer>
<script>
- const switches=document.querySelectorAll('.sw input');
- switches.forEach((sw,i)=>{
- sw.addEventListener('change',()=>{
- document.getElementById('s'+i).textContent=sw.checked?'On':'Off';
+ const stage=document.querySelector('.stage');
+ const boxes=[...stage.querySelectorAll('.sw input')];
+ const parts=[...stage.querySelectorAll('.sw-track,.sw-thumb')];
+ const slowTog=document.getElementById('slow-tog');
+ const vars=document.documentElement.style;
+ const rest=boxes.map(b=>b.defaultChecked); // the arrival state: Notifications on, Appearance off, Auto-save on
+ let dur=200,runTimers=[];
+
+ // Show me: one run at a time. Every timer of the run goes through later(); stopRun() cancels the whole run.
+ // later(fn, move, hold) waits move*(slow?3:1) + hold ms: Slow motion triples the movement, never the hold.
+ function later(fn,move,hold){
+ const id=setTimeout(()=>{runTimers=runTimers.filter(t=>t!==id);fn()},move*(slowTog.checked?3:1)+(hold||0));
+ runTimers.push(id);
+ }
+ function running(){return runTimers.length>0}
+ function stopRun(){runTimers.forEach(clearTimeout);runTimers=[]}
+ // A run starts from the arrival state, put there without animating: the transitions are turned off, the state is set,
+ // a reflow makes it stick, and the transitions come back.
+ function toRest(){
+ parts.forEach(p=>p.style.setProperty('transition','none','important'));
+ boxes.forEach((b,i)=>{b.checked=rest[i]});
+ void stage.offsetWidth;
+ parts.forEach(p=>p.style.removeProperty('transition'));
+ }
+ // Switch i slides to the other side (dur), the next one starts 200ms after it, and all three rest 600ms before they slide back.
+ document.getElementById('btn-demo').addEventListener('click',()=>{
+ stopRun();
+ toRest();
+ boxes.forEach((b,i)=>{
+ later(()=>{b.checked=!rest[i]},i*dur,i*200);
+ later(()=>{b.checked=rest[i]},(3+i)*dur,1000+i*200);
});
});
- document.getElementById('dur-sl').addEventListener('input',e=>{
- document.getElementById('dur-v').textContent=e.target.value+'ms';
- document.documentElement.style.setProperty('--tog-dur',e.target.value+'ms');
- });
- document.getElementById('ease-sel').addEventListener('change',e=>{
- document.documentElement.style.setProperty('--tog-ease',e.target.value);
- });
- document.getElementById('color-pick').addEventListener('input',e=>{
- document.documentElement.style.setProperty('--on-color',e.target.value);
- });
+ // The visitor pressed, typed, scrolled or touched inside the stage: the run stops and the switches stay as they are.
+ document.addEventListener('hb:input',()=>{if(running())stopRun()});
+
+ function choices(id,pick){
+ const buttons=document.querySelectorAll('#'+id+' button');
+ buttons.forEach(b=>b.addEventListener('click',()=>{
+ buttons.forEach(x=>{x.classList.toggle('on',x===b);x.setAttribute('aria-pressed',String(x===b))});
+ pick(b.dataset);
+ }));
+ }
+ choices('speed-seg',d=>{dur=+d.ms;vars.setProperty('--tog-dur',dur+'ms')});
+ choices('feel-seg',d=>{vars.setProperty('--tog-ease',d.ease)});
+ choices('color-sw',d=>{vars.setProperty('--on-color',d.c)});
document.getElementById('elastic-tog').addEventListener('change',e=>{
- const el=document.querySelector('.sw.elastic');
- if(e.target.checked){
- el.querySelector('.sw-thumb').style.transition='';
- }else{
- el.querySelector('.sw-thumb').style.transition=`transform var(--tog-dur) var(--tog-ease)`;
- }
+ document.getElementById('sw2').closest('.sw').classList.toggle('elastic',e.target.checked);
});
</script>
-<script>(function(){var b=document.getElementById("ah-copy");if(!b)return;b.addEventListener("click",function(){fetch(location.href).then(function(r){return r.text();}).catch(function(){return document.documentElement.outerHTML;}).then(function(t){return navigator.clipboard.writeText(t);}).then(function(){b.textContent="Copied!";b.classList.add("ah-copied");setTimeout(function(){b.textContent="Copy source";b.classList.remove("ah-copied");},1500);}).catch(function(){b.textContent="Copy failed";setTimeout(function(){b.textContent="Copy source";},1500);});});})();</script>
</body>
</html>
diff --git a/index.html b/index.html
index 91723b78..b19b7d62 100644
--- a/index.html
+++ b/index.html
@@ -956,7 +956,7 @@ const CATS=[
['focus-ring','Focus Ring Animation','A ring closes in around the item the Tab key reaches. Best for forms and menus.'],
['button-press-scale','Button Press Scale','Shrinks as you press it and springs back as you let go. Best for main buttons.'],
['magnetic-button','Magnetic Button','Leans toward the pointer, then springs home. Best for one main button.'],
- ['toggle-switch','Toggle / Switch Slide','Pill slides left/right for on/off — smooth cubic-bezier gives a weighted, physical feel.'],
+ ['toggle-switch','Toggle / Switch Slide','The knob slides across as the switch turns on or off. Best for settings.'],
['heart-burst','Heart / Like Burst','Tap pops and fills the heart while small hearts burst outward on canvas — Twitter-style like.'],
['success-confetti','Success Confetti','Completing an action fires a canvas confetti burst — rotating rectangles with gravity and spin.'],
['skeleton-loader','Skeleton Loader','Gray shapes hold the place of content while it loads. Best for feeds and cards.'],