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 Hamburger Menu Toggle to the guided-steps page

matin612c693360ed
diff --git a/animations/04-micro-interactions/hamburger-menu-toggle/README.md b/animations/04-micro-interactions/hamburger-menu-toggle/README.md
index 81b2e130..3f8d2e75 100644
--- a/animations/04-micro-interactions/hamburger-menu-toggle/README.md
+++ b/animations/04-micro-interactions/hamburger-menu-toggle/README.md
@@ -20,9 +20,10 @@ compositor.
```css
.bar{transition:transform var(--dur) var(--ease),opacity calc(var(--dur)*.6) var(--ease)}
-.burger[aria-expanded="true"] .bar.top{transform:translateY(16px) rotate(45deg)}
+/* Half the 36px box minus half a line, so the lines meet at any thickness */
+.burger[aria-expanded="true"] .bar.top{transform:translateY(calc(18px - var(--bar-h)/2)) rotate(45deg)}
.burger[aria-expanded="true"] .bar.mid{opacity:0;transform:scaleX(.2)}
-.burger[aria-expanded="true"] .bar.bot{transform:translateY(-16px) rotate(-45deg)}
+.burger[aria-expanded="true"] .bar.bot{transform:translateY(calc(var(--bar-h)/2 - 18px)) rotate(-45deg)}
```
The `translateY` value must equal the distance from each outer bar to the box
@@ -33,11 +34,9 @@ rotation pivot on that meeting point.
| Parameter | Default | Effect |
|-----------|---------|--------|
-| `--dur` | `340ms` | Length of the morph. Below ~200ms it snaps; above ~500ms it drags. |
-| `--ease` | `cubic-bezier(.65,0,.35,1)` | Timing curve. A back/overshoot curve adds a small spring on the cross. |
-| `--bar-w` | `44px` | Bar length; also the visual width of the icon. |
-| `--bar-h` | `4px` | Bar thickness. Thicker bars read better at small sizes. |
-| `translateY` | `16px` | Vertical travel that brings the outer bars together — tie it to the box height. |
+| Speed | Normal | How long the change takes: slow is 550ms, normal 340ms and fast 200ms; below about 200ms it snaps and above 500ms it drags |
+| Feel | Gentle | Gentle eases in and out; Springy lets the X overshoot a little, then settle; Smooth slows to a stop; Even keeps one steady speed |
+| Line thickness | Medium | The lines are 2px, 4px or 6px thick; thicker lines read better at small sizes |
## Production notes
Toggle `aria-expanded` on a real `<button>` (not a `<div>`) and update
@@ -52,6 +51,6 @@ morph from a hidden checkbox (`:checked ~`) instead of JavaScript, at the cost
of the ARIA state.
## See also
-- [Toggle / Switch Slide](../toggle-switch/)
-- [Drawer Slide](../drawer-slide/)
-- [Modal Expand](../modal-expand/)
+- [Toggle / Switch Slide](../toggle-switch/) — a switch that slides between on and off
+- [Drawer / Panel Slide](../drawer-slide/) — the side panel such a button often opens
+- [Modal Expand](../modal-expand/) — a window that grows out of the button you pressed
diff --git a/animations/04-micro-interactions/hamburger-menu-toggle/index.html b/animations/04-micro-interactions/hamburger-menu-toggle/index.html
index 6c923e17..b8af97ec 100644
--- a/animations/04-micro-interactions/hamburger-menu-toggle/index.html
+++ b/animations/04-micro-interactions/hamburger-menu-toggle/index.html
@@ -4,7 +4,7 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hamburger Menu Toggle — Animation Handbook</title>
- <meta name="description" content="Three bars morph into an ✕ — top and bottom rotate to cross while the middle fades out.">
+ <meta name="description" content="Three lines turn into an X as the menu opens. Best for mobile menus.">
<link rel="canonical" href="https://matinmonshizadeh.github.io/animation-handbook/animations/04-micro-interactions/hamburger-menu-toggle/">
<link rel="icon" type="image/png" sizes="96x96" href="../../../favicon.png">
<link rel="icon" type="image/svg+xml" href="../../../favicon.svg">
@@ -12,31 +12,24 @@
<meta property="og:type" content="article">
<meta property="og:site_name" content="Animation Handbook">
<meta property="og:title" content="Hamburger Menu Toggle — Animation Handbook">
- <meta property="og:description" content="Three bars morph into an ✕ — top and bottom rotate to cross while the middle fades out.">
+ <meta property="og:description" content="Three lines turn into an X as the menu opens. Best for mobile menus.">
<meta property="og:url" content="https://matinmonshizadeh.github.io/animation-handbook/animations/04-micro-interactions/hamburger-menu-toggle/">
<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="Hamburger Menu Toggle — Animation Handbook">
- <meta name="twitter:description" content="Three bars morph into an ✕ — top and bottom rotate to cross while the middle fades out.">
+ <meta name="twitter:description" content="Three lines turn into an X as the menu opens. Best for mobile menus.">
<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": "Hamburger Menu Toggle", "name": "Hamburger Menu Toggle", "description": "Three bars morph into an ✕ — top and bottom rotate to cross while the middle fades out.", "url": "https://matinmonshizadeh.github.io/animation-handbook/animations/04-micro-interactions/hamburger-menu-toggle/", "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, Hamburger Menu Toggle"}</script>
+ <script type="application/ld+json">{"@context": "https://schema.org", "@type": "TechArticle", "headline": "Hamburger Menu Toggle", "name": "Hamburger Menu Toggle", "description": "Three lines turn into an X as the menu opens. Best for mobile menus.", "url": "https://matinmonshizadeh.github.io/animation-handbook/animations/04-micro-interactions/hamburger-menu-toggle/", "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, Hamburger Menu Toggle"}</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;--dur:340ms;--ease:cubic-bezier(.65,0,.35,1);--bar-h:4px;--bar-w:44px}
+ :root{--ui-accent:#ff9d5c;--ui-bg:#111114;--ui-border:rgba(255,255,255,.09);--dur:340ms;--ease:cubic-bezier(.65,0,.35,1);--bar-h:4px;--bar-w:44px}
*,*::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-wrap{flex:1;min-width:0}
- .stage{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:28px;padding:32px}
+ .stage{display:flex;flex-direction:column;align-items:center;justify-content:center;padding:32px;background:#0b0b0d}
+ /* The button and the menu move as one pair. At rest the pair sits half the menu and its gap lower, so the button is in the middle of
+ the stage; opening the menu slides the pair back up, as the button rises when the menu grows. Transforms only. */
+ .pair{display:flex;flex-direction:column;align-items:center;gap:20px;width:100%;transform:translateY(calc(50% - 38px));transition:transform var(--dur) var(--ease)}
+ .pair:has(> .menu.show){transform:none}
/* The button */
- .burger{position:relative;width:76px;height:76px;flex-shrink:0;display:grid;place-items:center;background:var(--ui-bg);border:1px solid var(--ui-border);border-radius:14px;cursor:pointer;-webkit-tap-highlight-color:transparent;touch-action:manipulation;transition:border-color var(--dur) var(--ease)}
- .burger:focus-visible{outline:2px solid var(--ui-accent);outline-offset:3px}
+ .burger{position:relative;width:76px;height:76px;flex-shrink:0;display:grid;place-items:center;background:var(--ui-bg);border:1px solid var(--ui-border);border-radius:14px;font-family:inherit;cursor:pointer;-webkit-tap-highlight-color:transparent;touch-action:manipulation;transition:border-color var(--dur) var(--ease)}
.bars{position:relative;width:var(--bar-w);height:36px}
.bar{position:absolute;left:0;width:100%;height:var(--bar-h);border-radius:2px;background:var(--ui-accent);transition:transform var(--dur) var(--ease),opacity calc(var(--dur)*.6) var(--ease)}
.bar.top{top:0;transform-origin:center}
@@ -48,9 +41,9 @@
.burger[aria-expanded="true"] .bar.mid{opacity:0;transform:scaleX(.2)}
.burger[aria-expanded="true"] .bar.bot{transform:translateY(calc(var(--bar-h)/2 - 18px)) rotate(-45deg)}
.burger[aria-expanded="true"]{border-color:var(--ui-accent)}
- /* Slide-down menu */
- .menu{width:100%;max-width:280px;overflow:hidden;max-height:0;opacity:0;transition:max-height var(--dur) var(--ease),opacity var(--dur) var(--ease)}
- .menu.show{max-height:240px;opacity:1}
+ /* The menu keeps its full height in the layout and opens with opacity and transform; closed, it is also out of reach of Tab and screen readers */
+ .menu{flex-shrink:0;width:100%;max-width:280px;opacity:0;transform:translateY(-8px);visibility:hidden;transition:opacity var(--dur) var(--ease),transform var(--dur) var(--ease),visibility 0s linear var(--dur)}
+ .menu.show{opacity:1;transform:none;visibility:visible;transition:opacity var(--dur) var(--ease),transform var(--dur) var(--ease),visibility 0s}
.menu ul{list-style:none;display:flex;flex-direction:column;gap:6px}
.menu li{background:var(--ui-bg);border:1px solid var(--ui-border);border-radius:6px;padding:12px 14px;font-size:12px;transform:translateY(-8px);opacity:0;transition:transform var(--dur) var(--ease),opacity var(--dur) var(--ease)}
.menu.show li{transform:translateY(0);opacity:1}
@@ -58,97 +51,190 @@
.menu.show li:nth-child(2){transition-delay:110ms}
.menu.show li:nth-child(3){transition-delay:160ms}
.menu.show li:nth-child(4){transition-delay:210ms}
- .state{font-size:10px;color:var(--ui-muted)}
- .state b{color:var(--ui-accent);font-weight:500}
- /* Controls */
- 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:52px;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}
@media(hover:hover){.burger:hover{border-color:var(--ui-accent)}}
- @media(max-width:600px){.layout{flex-direction:column}.stage-wrap,aside{width:100%}:root{--stage-h:auto}.stage{height:auto;min-height:400px}}
- @media(prefers-reduced-motion:reduce){.bar,.menu,.menu li,.burger{transition:none!important}.menu li{transition-delay:0ms!important}}
+ /* A window this short gives the stage 260px, less than the open button and menu need (278px): the gaps and the items give up some space so both fit */
+ @media(min-width:601px) and (max-height:640px){.pair{gap:12px}.menu ul{gap:4px}.menu li{padding:10px 14px}}
+ @media(prefers-reduced-motion:reduce){.bar,.menu,.menu li,.burger,.pair{transition:none!important}.menu li{transition-delay:0ms!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="../swipe-to-dismiss/" rel="prev" title="Previous">‹ Prev</a><a class="ah-hideSm" href="../theme-toggle-morph/" 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/hamburger-menu-toggle/index.html" target="_blank" rel="noopener noreferrer">Source ↗</a></span></div>
-<header>
- <h1>Hamburger Menu Toggle</h1>
- <p>Three bars morph into an ✕ — top and bottom rotate to cross while the middle fades out.</p>
-</header>
-<div class="layout">
- <div class="stage-wrap">
- <div class="stage">
- <button type="button" class="burger" id="burger" aria-label="Open menu" aria-expanded="false" aria-controls="menu">
- <span class="bars" aria-hidden="true">
- <span class="bar top"></span>
- <span class="bar mid"></span>
- <span class="bar bot"></span>
- </span>
- </button>
- <nav class="menu" id="menu" aria-label="Demo menu">
- <ul>
- <li>Home</li>
- <li>Work</li>
- <li>About</li>
- <li>Contact</li>
- </ul>
- </nav>
- <div class="state">State: <b id="state">closed</b></div>
+<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="../swipe-to-dismiss/" rel="prev" aria-label="Previous: Swipe to Dismiss"><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>Swipe to Dismiss</span></a><a href="../theme-toggle-morph/" rel="next" aria-label="Next: Theme Toggle Morph"><span><span class="hb-dir">Next: </span>Theme Toggle Morph</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.23 · Micro-Interactions</p>
+ <h1>Hamburger Menu Toggle</h1>
+ <p class="hb-lede">Three lines turn into an X as the menu opens. Best for mobile menus.</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>Press the button to open the menu and again to close it, or press Show me.</p></div>
</div>
- </div>
- <aside>
- <p class="note">The icon and the ✕ are the same three bars. Nothing is added or removed — only <b style="color:var(--ui-accent)">transform</b> and <b style="color:var(--ui-accent)">opacity</b> change, so it stays on the GPU.</p>
- <div class="lbl">Duration</div>
- <div class="sr"><input type="range" id="dur-sl" min="120" max="700" value="340" step="20"><span class="sval" id="dur-v">340ms</span></div>
- <div class="lbl">Bar thickness</div>
- <div class="sr"><input type="range" id="th-sl" min="2" max="7" value="4" step="1"><span class="sval" id="th-v">4px</span></div>
- <div class="lbl">Easing</div>
- <select id="ease-sel">
- <option value="cubic-bezier(.65,0,.35,1)" selected>Smooth (in-out)</option>
- <option value="cubic-bezier(.34,1.56,.64,1)">Back / overshoot</option>
- <option value="ease-out">Ease out</option>
- <option value="linear">Linear</option>
- </select>
- <div class="div"></div>
- <p style="font-size:10px;color:var(--ui-muted)">Tap the button to toggle. It works with keyboard too — Tab to it, press Enter.</p>
- </aside>
-</div>
+ <div class="stage hb-dots">
+ <div class="pair">
+ <button type="button" class="burger" id="burger" aria-label="Open menu" aria-expanded="false" aria-controls="menu">
+ <span class="bars" aria-hidden="true">
+ <span class="bar top"></span>
+ <span class="bar mid"></span>
+ <span class="bar bot"></span>
+ </span>
+ </button>
+ <nav class="menu" id="menu" aria-label="Demo menu">
+ <ul>
+ <li>Home</li>
+ <li>Work</li>
+ <li>About</li>
+ <li>Contact</li>
+ </ul>
+ </nav>
+ </div>
+ </div>
+ <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>
+ </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 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="550" aria-pressed="false">Slow</button>
+ <button type="button" data-ms="340" class="on" aria-pressed="true">Normal</button>
+ <button type="button" data-ms="200" aria-pressed="false">Fast</button>
+ </div>
+ <p class="hb-hint">How long the lines take to turn into an X.</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="cubic-bezier(.65,0,.35,1)" class="on" aria-pressed="true">Gentle</button>
+ <button type="button" data-ease="cubic-bezier(.34,1.56,.64,1)" aria-pressed="false">Springy</button>
+ <button type="button" data-ease="ease-out" aria-pressed="false">Smooth</button>
+ <button type="button" data-ease="linear" aria-pressed="false">Even</button>
+ </div>
+ <p class="hb-hint">Springy lets the X overshoot a little, then settle.</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">
+ <p class="hb-setting-name" id="thick-lbl">Line thickness</p>
+ <div class="seg" id="thick-seg" role="group" aria-labelledby="thick-lbl">
+ <button type="button" data-px="2" aria-pressed="false">Thin</button>
+ <button type="button" data-px="4" class="on" aria-pressed="true">Medium</button>
+ <button type="button" data-px="6" aria-pressed="false">Thick</button>
+ </div>
+ <p class="hb-hint">Thicker lines read better at small sizes.</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 a menu button whose three lines turn into an X to [the menu button in your header]. When it is pressed, the top and bottom lines move to the middle and turn to cross each other while the middle line fades away, and pressing it again reverses the change. Use the same three lines for both states, moving and turning them rather than swapping icons, and move each outer line exactly to the middle so the X meets at its center. Make it a real button that tells screen readers whether the menu is open. If the visitor has reduced motion turned on, switch between the two icons without movement. 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>Mobile menus</li><li>Compact headers</li><li>Side panels</li></ul>
+ <h3>Avoid on</h3>
+ <ul class="hb-tags hb-avoid"><li>Wide desktop menus</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 stage=document.querySelector('.stage');
const btn=document.getElementById('burger');
const menu=document.getElementById('menu');
- const state=document.getElementById('state');
- btn.addEventListener('click',()=>{
- const open=btn.getAttribute('aria-expanded')==='true';
- btn.setAttribute('aria-expanded',String(!open));
- btn.setAttribute('aria-label',open?'Open menu':'Close menu');
- menu.classList.toggle('show',!open);
- state.textContent=open?'closed':'open';
+ const slowTog=document.getElementById('slow-tog');
+ const vars=document.documentElement.style;
+ // Everything on the stage that moves, so a run can start from rest without animating
+ const moving=[...stage.querySelectorAll('.pair,.burger,.bar,.menu,.menu li')];
+ let dur=340,runTimers=[];
+
+ // The button and Show me both open and close the menu through toggle().
+ function setOpen(on){
+ btn.setAttribute('aria-expanded',String(on));
+ btn.setAttribute('aria-label',on?'Close menu':'Open menu');
+ menu.classList.toggle('show',on);
+ }
+ function toggle(){setOpen(btn.getAttribute('aria-expanded')!=='true')}
+ btn.addEventListener('click',toggle);
+
+ // 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 with the menu closed, put there without animating: the transitions are turned off, the menu is closed,
+ // a reflow makes it stick, and the transitions come back.
+ function toRest(){
+ moving.forEach(el=>el.style.setProperty('transition','none','important'));
+ setOpen(false);
+ void stage.offsetWidth;
+ moving.forEach(el=>el.style.removeProperty('transition'));
+ }
+ // The menu opens as a press opens it, and closes once the lines and the last item (which starts 210ms late) have arrived and 1200ms have passed.
+ document.getElementById('btn-demo').addEventListener('click',()=>{
+ stopRun();
+ toRest();
+ toggle();
+ later(toggle,dur+210,1200);
});
- const root=document.documentElement;
- const durSl=document.getElementById('dur-sl'),durV=document.getElementById('dur-v');
- durSl.addEventListener('input',e=>{durV.textContent=e.target.value+'ms';root.style.setProperty('--dur',e.target.value+'ms');});
- const thSl=document.getElementById('th-sl'),thV=document.getElementById('th-v');
- thSl.addEventListener('input',e=>{thV.textContent=e.target.value+'px';root.style.setProperty('--bar-h',e.target.value+'px');});
- document.getElementById('ease-sel').addEventListener('change',e=>{root.style.setProperty('--ease',e.target.value);});
+ // The visitor pressed, typed, scrolled or touched inside the stage: the run stops and the menu stays as it is.
+ 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('--dur',dur+'ms')});
+ choices('feel-seg',d=>{vars.setProperty('--ease',d.ease)});
+ choices('thick-seg',d=>{vars.setProperty('--bar-h',d.px+'px')});
</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 b19b7d62..52a365fd 100644
--- a/index.html
+++ b/index.html
@@ -973,7 +973,7 @@ const CATS=[
['cursor-follower','Cursor Follower','Custom element follows mouse with lerp lag — mix-blend-mode: difference inverts any background.'],
['error-shake','Error Shake','An invalid field shakes horizontally with a decaying wobble and flashes red — the universal "no".'],
['swipe-to-dismiss','Swipe to Dismiss','Drag a card past a threshold and it flies off as the row collapses — pointer-driven, touch-ready.'],
- ['hamburger-menu-toggle','Hamburger Menu Toggle','The bars icon morphs to an X — top and bottom bars rotate to cross while the middle fades out.'],
+ ['hamburger-menu-toggle','Hamburger Menu Toggle','Three lines turn into an X as the menu opens. Best for mobile menus.'],
['theme-toggle-morph','Theme Toggle Morph','A sun morphs into a crescent moon as the interface flips between light and dark.'],
['copy-to-clipboard','Copy to Clipboard','The copy button swaps to a checkmark and a Copied confirmation, then reverts after a moment.'],
['star-rating','Star Rating','Stars fill toward the pointer and pop on commit — an accessible five-star control.'],