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 3D Flip Card to the guided-steps page

matin0d18a4f58b5a
diff --git a/animations/06-3d-advanced/flip-card-3d/README.md b/animations/06-3d-advanced/flip-card-3d/README.md
index af42ff81..c9db0129 100644
--- a/animations/06-3d-advanced/flip-card-3d/README.md
+++ b/animations/06-3d-advanced/flip-card-3d/README.md
@@ -1,7 +1,7 @@
# 3D Flip Card
## What it is
-A flip card is a single element with a front and back face that rotates in three dimensions to swap which side is visible. The rotation happens in real 3D space rather than a 2D crossfade: a `perspective` on the container gives the turn depth, `transform-style: preserve-3d` keeps both faces in the same 3D scene, and `backface-visibility: hidden` hides whichever side is turned away from the viewer. An optional cursor-driven tilt while the card is idle adds a sense of physical depth before the flip.
+A 3D flip card has a front and a back and turns over in 3D to show the other side. Both faces sit in the same place, the back one turned away, and whichever face points away from the viewer is hidden. Perspective on the card's container gives the turn real depth, so it feels like turning over a physical card.
## When to use it
- Profile, team, or contact cards where the back holds secondary details
@@ -52,11 +52,11 @@ stage.addEventListener('pointermove', e => {
## Key parameters
| Parameter | Default | Effect |
|-----------|---------|--------|
-| Flip axis | rotateY | `rotateY` turns left/right; `rotateX` turns top/bottom. Match the back face's pre-rotation to the axis. |
-| Flip duration | 650ms | 150–300ms feels snappy; 600–900ms feels physical; over ~1200ms drags. |
-| Perspective depth | 1200px | Lower (500px) exaggerates the turn into a fish-eye; higher (2500px) flattens it toward a 2D spin. |
-| Idle tilt strength | 10° | Degrees of pre-flip lean toward the cursor. 0 disables it; over ~20° feels unstable. |
-| Trigger | Click | Click/tap toggles; hover flips on pointer enter and restores on leave. |
+| Flip direction | Sideways | Sideways turns the card like a page; up and over turns it like a flap |
+| Speed | Normal | How long one turn takes: slow is 1000ms, normal 650ms and fast 400ms; 600 to 900ms feels physical |
+| 3D depth | Medium | How close the viewer seems: subtle is 2000px, medium 1200px and strong 600px; closer exaggerates the turn |
+| Flips on hover | off | Pointing at the card turns it over and leaving turns it back; a tap still turns it on touch screens |
+| Tilt toward the pointer | Gentle | While idle, the card leans toward a hovering pointer, up to 5° (gentle) or 9° (strong) at the stage's edges; off keeps it flat |
## Production notes
- **Backface-visibility support**: both faces must be absolutely positioned and stacked, and the inner element must have `preserve-3d`. If `preserve-3d` is missing (or a property like `overflow` or `filter` flattens the context), both faces render and the back bleeds through.
@@ -66,6 +66,6 @@ stage.addEventListener('pointermove', e => {
- **Libraries**: Framer Motion animates this with `rotateY` on a motion component and `AnimatePresence` for the face swap; GSAP handles it with `rotationY` plus `transformPerspective`/`transformStyle`. The vanilla CSS version above needs no dependency for the common case.
## See also
-- [Parallax 3D Tilt](../parallax-3d-tilt/)
-- [Scroll-Driven 3D Rotation](../scroll-driven-3d-rotation/)
-- [Glassmorphism Animated](../glassmorphism-animated/)
+- [Parallax 3D Tilt](../parallax-3d-tilt/) — a card leans toward the pointer without turning over
+- [Flip In](../../02-entrance-and-exit/flip-in/) — a card swings into view on a 3D hinge
+- [Scroll-Driven 3D Rotation](../scroll-driven-3d-rotation/) — scrolling turns a 3D cube
diff --git a/animations/06-3d-advanced/flip-card-3d/index.html b/animations/06-3d-advanced/flip-card-3d/index.html
index 5aa15d03..103baac8 100644
--- a/animations/06-3d-advanced/flip-card-3d/index.html
+++ b/animations/06-3d-advanced/flip-card-3d/index.html
@@ -4,7 +4,7 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>3D Flip Card — Animation Handbook</title>
- <meta name="description" content="A card rotates in 3D on rotateY/rotateX to reveal its back face, with an optional cursor-driven idle tilt for depth.">
+ <meta name="description" content="A card turns over in 3D to show its back. Best for profile and product cards.">
<link rel="canonical" href="https://matinmonshizadeh.github.io/animation-handbook/animations/06-3d-advanced/flip-card-3d/">
<link rel="icon" type="image/png" sizes="96x96" href="../../../favicon.png">
<link rel="icon" type="image/svg+xml" href="../../../favicon.svg">
@@ -12,43 +12,32 @@
<meta property="og:type" content="article">
<meta property="og:site_name" content="Animation Handbook">
<meta property="og:title" content="3D Flip Card — Animation Handbook">
- <meta property="og:description" content="A card rotates in 3D on rotateY/rotateX to reveal its back face, with an optional cursor-driven idle tilt for depth.">
+ <meta property="og:description" content="A card turns over in 3D to show its back. Best for profile and product cards.">
<meta property="og:url" content="https://matinmonshizadeh.github.io/animation-handbook/animations/06-3d-advanced/flip-card-3d/">
<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="3D Flip Card — Animation Handbook">
- <meta name="twitter:description" content="A card rotates in 3D on rotateY/rotateX to reveal its back face, with an optional cursor-driven idle tilt for depth.">
+ <meta name="twitter:description" content="A card turns over in 3D to show its back. Best for profile and product cards.">
<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": "3D Flip Card", "name": "3D Flip Card", "description": "A card rotates in 3D on rotateY/rotateX to reveal its back face, with an optional cursor-driven idle tilt for depth.", "url": "https://matinmonshizadeh.github.io/animation-handbook/animations/06-3d-advanced/flip-card-3d/", "inLanguage": "en", "articleSection": "3D & Advanced", "isPartOf": {"@type": "WebSite", "name": "Animation Handbook", "url": "https://matinmonshizadeh.github.io/animation-handbook/"}, "author": {"@type": "Person", "name": "matinmonshizadeh"}, "keywords": "web animation, CSS animation, 3D & Advanced, 3D Flip Card"}</script>
+ <script type="application/ld+json">{"@context": "https://schema.org", "@type": "TechArticle", "headline": "3D Flip Card", "name": "3D Flip Card", "description": "A card turns over in 3D to show its back. Best for profile and product cards.", "url": "https://matinmonshizadeh.github.io/animation-handbook/animations/06-3d-advanced/flip-card-3d/", "inLanguage": "en", "articleSection": "3D & Advanced", "isPartOf": {"@type": "WebSite", "name": "Animation Handbook", "url": "https://matinmonshizadeh.github.io/animation-handbook/"}, "author": {"@type": "Person", "name": "matinmonshizadeh"}, "keywords": "web animation, CSS animation, 3D & Advanced, 3D Flip Card"}</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:#b98cff;--ui-text:#f4f4f2;--ui-muted:#77777e;--disp:'Bricolage','Arial Narrow',sans-serif;--mono:'PlexMono',ui-monospace,Menlo,Consolas,monospace;--perspective:1200px;--flip-dur:650ms;--tilt:10}
+ :root{--ui-accent:#3fd6c4;--perspective:1200px;--flip-dur:650ms}
*,*::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:clamp(360px,52vh,560px);border:1px solid var(--ui-border);border-radius:8px;background:radial-gradient(120% 120% at 50% 0%,#141018,#08070a);display:flex;align-items:center;justify-content:center;padding:clamp(20px,4vw,48px);perspective:var(--perspective)}
- /* Tilt wrapper carries the idle cursor-driven 3D tilt */
- .tilt{width:100%;display:flex;justify-content:center;transform-style:preserve-3d;transition:transform 300ms ease}
+ .stage{background:radial-gradient(120% 120% at 50% 0%,#141018,#08070a);display:flex;align-items:center;justify-content:center;padding:clamp(20px,4vw,48px);perspective:var(--perspective)}
+ /* A window under 657px tall gives the stage 260 to 326px, so the stage gives up padding as the window shrinks and the card
+ keeps the 156×218 it has on a 1366×657 laptop, the smallest size that holds its text on one line */
+ @media(min-width:601px) and (max-height:656px){.stage{padding:clamp(14px,calc(50vh - 281px),48px);padding:clamp(14px,calc(50svh - 281px),48px)}}
+ /* Tilt wrapper carries the idle cursor-driven 3D tilt; it fills the stage so the card sits on the middle of the perspective.
+ It lies in the same plane as the card, so a tilted wrapper would win some hit tests and swallow clicks meant for the card:
+ only the card takes the pointer. */
+ .tilt{width:100%;height:100%;display:flex;align-items:center;justify-content:center;transform-style:preserve-3d;transition:transform 300ms ease;pointer-events:none}
.tilt.tracking{transition:none}
- /* Card — the flipping element */
- .card{
- /* capped by the stage height so the 5/7 card never spills past the stage border */
- width:min(clamp(220px,60%,300px),calc((clamp(360px,52vh,560px) - 40px) * 5 / 7));
- aspect-ratio:5/7;position:relative;
- transform-style:preserve-3d;cursor:pointer;
- transition:transform var(--flip-dur) cubic-bezier(.6,.02,.2,1);
- will-change:transform
- }
+ /* Card — the flipping element, sized from the stage's height (5:7, at most 340px tall) */
+ .card{flex:none;height:min(95%,340px);width:auto;aspect-ratio:5/7;position:relative;transform-style:preserve-3d;cursor:pointer;pointer-events:auto;
+ transition:transform var(--flip-dur) cubic-bezier(.6,.02,.2,1);will-change:transform}
.card.flipped{transform:rotateY(180deg)}
.card.axis-x.flipped{transform:rotateX(180deg)}
- .card:focus-visible{outline:2px solid var(--ui-accent);outline-offset:6px;border-radius:16px}
+ .hb-page .stage .card:focus-visible{outline:2px solid var(--ui-accent);outline-offset:6px;border-radius:16px}
.face{
position:absolute;inset:0;border-radius:16px;overflow:hidden;
backface-visibility:hidden;-webkit-backface-visibility:hidden;
@@ -61,34 +50,21 @@
.front{background:linear-gradient(150deg,#2a1a4a,#1a1030)}
.card.axis-x .back{transform:rotateX(180deg)}
.card:not(.axis-x) .back{transform:rotateY(180deg)}
- .avatar{width:56px;height:56px;border-radius:50%;background:linear-gradient(135deg,#b98cff,#6d4ea8);display:flex;align-items:center;justify-content:center;font-family:var(--disp);font-weight:800;font-size:22px;color:#0b0b0d}
- .front .name{font-family:var(--disp);font-weight:700;font-size:clamp(16px,2.4vw,22px);margin-top:auto;color:#fff;line-height:1.15}
- .front .role{font-size:11px;color:var(--ui-accent);letter-spacing:1px;margin-top:6px;text-transform:uppercase}
- .front .hint{font-size:9px;color:rgba(255,255,255,.4);margin-top:14px;letter-spacing:.5px}
+ .avatar{width:56px;height:56px;border-radius:50%;background:linear-gradient(135deg,#b98cff,#6d4ea8);display:flex;align-items:center;justify-content:center;font-weight:800;font-size:22px;color:#0b0b0d}
+ .front .name{font-weight:700;font-size:clamp(16px,2.4vw,22px);margin-top:auto;color:#fff;line-height:1.15}
+ .front .role{font-size:11px;color:#b98cff;letter-spacing:1px;margin-top:6px;text-transform:uppercase}
+ .front .hint{font-size:11px;color:rgba(255,255,255,.6);margin-top:14px;letter-spacing:.5px}
/* Back — details face */
.back{background:linear-gradient(150deg,#120c1e,#0d0a16)}
- .back .over{font-size:9px;letter-spacing:2px;text-transform:uppercase;color:var(--ui-accent)}
+ .back .over{font-size:11px;font-weight:600;letter-spacing:2px;text-transform:uppercase;color:#b98cff}
.back .row{display:flex;justify-content:space-between;font-size:11px;color:rgba(255,255,255,.7);padding:8px 0;border-bottom:1px solid rgba(255,255,255,.07)}
.back .row span:last-child{color:#fff}
.back .rows{margin-top:auto}
- .back .hint{font-size:9px;color:rgba(255,255,255,.4);margin-top:14px;letter-spacing:.5px}
- /* Panel */
- 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)}
- .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}
- .sv{width:52px;text-align:right;color:var(--ui-text)}
- .seg{display:flex;gap:4px}
- .seg button{flex:1;min-height:44px;background:#08070a;border:1px solid var(--ui-border);color:var(--ui-muted);border-radius:6px;font:inherit;font-size:10px;cursor:pointer}
- .seg button.on{border-color:var(--ui-accent);color:var(--ui-text)}
- .div{height:1px;background:var(--ui-border)}
- .flip-btn{min-height:44px;background:var(--ui-accent);border:0;border-radius:6px;color:#0b0b0d;font:inherit;font-weight:600;font-size:11px;cursor:pointer}
- @media(max-width:600px){.layout{flex-direction:column;align-items:stretch}aside{width:100%}}
+ .back .hint{font-size:11px;color:rgba(255,255,255,.6);margin-top:14px;letter-spacing:.5px}
/* Tilt only on true hover-capable pointers */
@media(hover:hover){.tilt{will-change:transform}}
@media(hover:none){.tilt{transform:none!important}}
- /* Reduced motion — instant opacity swap, no tilt, no rotation */
+ /* Reduced motion — a quick fade between the faces, no tilt, no rotation */
@media(prefers-reduced-motion:reduce){
.card{transition:none!important;transform:none!important}
.tilt{transition:none!important;transform:none!important}
@@ -99,28 +75,26 @@
.card.flipped .back{opacity:1}
}
</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="../morphing-blob/" rel="prev" title="Previous">‹ Prev</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/06-3d-advanced/flip-card-3d/index.html" target="_blank" rel="noopener noreferrer">Source ↗</a></span></div>
-<header>
- <h1>3D Flip Card</h1>
- <p>A card rotates in 3D on rotateY/rotateX to reveal its back face, with an optional cursor-driven idle tilt for depth.</p>
-</header>
-<div class="layout">
- <div class="stage-wrap">
+<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="../morphing-blob/" rel="prev" aria-label="Previous: Morphing Blob"><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>Morphing Blob</span></a></span>
+</nav>
+<main class="hb-page">
+ <header class="hb-head">
+ <p class="hb-cat">06.18 · 3D &amp; Advanced</p>
+ <h1>3D Flip Card</h1>
+ <p class="hb-lede">A card turns over in 3D to show its back. Best for profile and product cards.</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 the card to turn it over, or press Show me.</p></div>
+ </div>
<div class="stage" id="stage">
<div class="tilt" id="tilt">
<div class="card" id="card" tabindex="0" role="button" aria-pressed="false" aria-label="Flip card to see details">
@@ -128,7 +102,7 @@ body{padding-top:46px}
<div class="avatar">AV</div>
<div class="name">Ava Reyes</div>
<div class="role">Motion Engineer</div>
- <div class="hint">Click / tap / Enter to flip →</div>
+ <div class="hint">Tap or click to flip →</div>
</div>
<div class="face back">
<div class="over">Details</div>
@@ -143,35 +117,113 @@ body{padding-top:46px}
</div>
</div>
</div>
- </div>
- <aside>
- <p class="note">perspective sits on the stage. The inner card uses transform-style:preserve-3d; each face is backface-visibility:hidden so only the facing side shows.</p>
- <div class="lbl">Flip axis</div>
- <div class="seg" id="axis">
- <button data-axis="y" class="on">rotateY</button>
- <button data-axis="x">rotateX</button>
+ <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>
+ <button class="hb-play" type="button" id="btn-reset" data-hb-reset><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="M3 12a9 9 0 1 0 9-9 9.75 9.75 0 0 0-6.74 2.74L3 8"/><path d="M3 3v5h5"/></svg>Reset</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="axis-lbl">Flip direction</p>
+ <div class="seg" id="axis-seg" role="group" aria-labelledby="axis-lbl">
+ <button type="button" data-axis="y" class="on" aria-pressed="true">Sideways</button>
+ <button type="button" data-axis="x" aria-pressed="false">Up and over</button>
+ </div>
+ <p class="hb-hint">Sideways turns like a page; up and over like a flap.</p>
+ </div>
+ <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="1000" aria-pressed="false">Slow</button>
+ <button type="button" data-ms="650" class="on" aria-pressed="true">Normal</button>
+ <button type="button" data-ms="400" aria-pressed="false">Fast</button>
+ </div>
+ <p class="hb-hint">How long one turn takes.</p>
+ </div>
+ <div class="hb-setting">
+ <p class="hb-setting-name" id="depth-lbl">3D depth</p>
+ <div class="seg" id="depth-seg" role="group" aria-labelledby="depth-lbl">
+ <button type="button" data-px="2000" aria-pressed="false">Subtle</button>
+ <button type="button" data-px="1200" class="on" aria-pressed="true">Medium</button>
+ <button type="button" data-px="600" aria-pressed="false">Strong</button>
+ </div>
+ <p class="hb-hint">Strong looks closer and more dramatic.</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>Flips on hover</span><input class="hb-switch" type="checkbox" role="switch" id="hover-tog" autocomplete="off"></label>
+ <p class="hb-hint">Pointing flips it; a tap still flips it on phones.</p>
+ </div>
+ <div class="hb-setting">
+ <p class="hb-setting-name" id="tilt-lbl">Tilt toward the pointer</p>
+ <div class="seg" id="tilt-seg" role="group" aria-labelledby="tilt-lbl">
+ <button type="button" data-deg="0" aria-pressed="false">Off</button>
+ <button type="button" data-deg="10" class="on" aria-pressed="true">Gentle</button>
+ <button type="button" data-deg="18" aria-pressed="false">Strong</button>
+ </div>
+ <p class="hb-hint">The card leans toward a hovering pointer.</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="lbl">Trigger</div>
- <div class="seg" id="trigger">
- <button data-trig="click" class="on">Click</button>
- <button data-trig="hover">Hover</button>
+ <div class="hb-card">
+ <p class="hb-prompt">Add a 3D flip to [the card you want to turn over]. Place the front and back faces in the same spot, turn the back face around so it faces away, and hide whichever face points away from the viewer. Clicking, tapping or pressing Enter turns the card over in 3D to show its back, and again to show its front. Give the card's container a 3D perspective so the turn has depth. When the settings include it, pointing at the card can flip it too, and the card can lean toward a hovering pointer. If the visitor has reduced motion turned on, swap the faces with a quick fade instead of turning. 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>
- <div class="lbl">Flip duration</div>
- <div class="sr"><input type="range" id="dur-sl" min="150" max="1400" value="650" step="50"><span class="sv" id="dur-v">650ms</span></div>
- <div class="lbl">Perspective depth</div>
- <div class="sr"><input type="range" id="persp-sl" min="500" max="2500" value="1200" step="50"><span class="sv" id="persp-v">1200px</span></div>
- <div class="lbl">Idle tilt strength</div>
- <div class="sr"><input type="range" id="tilt-sl" min="0" max="20" value="10" step="1"><span class="sv" id="tilt-v">10°</span></div>
- <div class="div"></div>
- <button class="flip-btn" id="flip-btn">Flip card</button>
- </aside>
-</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>Profile cards</li><li>Product cards</li><li>Flashcards</li><li>Quiz answers</li></ul>
+ <h3>Avoid on</h3>
+ <ul class="hb-tags hb-avoid"><li>Long text</li><li>Key information</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.getElementById('stage');
const tilt=document.getElementById('tilt');
const card=document.getElementById('card');
+ const slowTog=document.getElementById('slow-tog');
const root=document.documentElement.style;
- let axis='y',trigger='click',maxTilt=10;
+ const reduce=matchMedia('(prefers-reduced-motion:reduce)');
+ const canHover=matchMedia('(hover:hover)');
+ const FLAT='rotateX(0deg) rotateY(0deg)';
+ let trigger='click',maxTilt=10,flipMs=650,runTimer=0;
function setFlipped(on){
card.classList.toggle('flipped',on);
@@ -179,63 +231,83 @@ body{padding-top:46px}
}
function toggle(){setFlipped(!card.classList.contains('flipped'));}
- // Trigger: click
- // Hover trigger gives a touch a way in: a tap flips even in hover mode.
- // Gate on the pointer that actually acted, not on the device — a touch
- // laptop reports hover:hover, so a media query alone leaves tap dead there.
+ // Show me: one click's worth. The card turns to its back, rests there for 1.2 s and turns back. Slow motion stretches the
+ // turns (the wait for the first one is three times as long), never the rest. A run starts from the front: a card that
+ // shows its back is put there without turning. The one timer is the run's only handle; anything can stop it.
+ function stopRun(){clearTimeout(runTimer);runTimer=0;}
+ function toFront(){
+ if(!card.classList.contains('flipped'))return;
+ card.style.setProperty('transition','none','important');
+ setFlipped(false);void card.offsetWidth;
+ card.style.removeProperty('transition');
+ }
+ document.getElementById('btn-demo').addEventListener('click',()=>{
+ stopRun();toFront();setFlipped(true);
+ runTimer=setTimeout(()=>{runTimer=0;setFlipped(false);},flipMs*(slowTog.checked?3:1)+1200);
+ });
+ document.getElementById('btn-reset').addEventListener('click',()=>{
+ stopRun();setFlipped(false);tilt.style.transform=FLAT;
+ });
+ // The visitor pressed, typed, scrolled or touched inside the stage: only a run that is under way stops, and the card stays
+ // as it is. The click, tap or key that comes with it then turns the card as usual.
+ document.addEventListener('hb:input',()=>{if(runTimer)stopRun();});
+
+ // Click or tap: gate on the pointer that actually acted, not on the device, since a touch laptop reports hover:hover and a
+ // media query alone would leave a tap dead there. With Flips on hover a tap still turns the card.
let lastPointer='mouse';
card.addEventListener('pointerdown',e=>{lastPointer=e.pointerType;});
card.addEventListener('click',()=>{if(trigger==='click'||lastPointer==='touch'||!canHover.matches)toggle();});
- document.getElementById('flip-btn').addEventListener('click',toggle);
- // Keyboard — flip is reachable and operable without a pointer
+ // Keyboard — the card is reachable and operable without a pointer
card.addEventListener('keydown',e=>{
- if(e.key==='Enter'||e.key===' '){e.preventDefault();toggle();}
+ if((e.key==='Enter'||e.key===' ')&&!e.repeat){e.preventDefault();toggle();}
});
- // Trigger: hover (pointer-capable devices only)
- card.addEventListener('pointerenter',e=>{if(trigger==='hover'&&e.pointerType!=='touch')setFlipped(true);});
- card.addEventListener('pointerleave',e=>{if(trigger==='hover'&&e.pointerType!=='touch')setFlipped(false);});
+ // Flips on hover (pointer-capable devices only): the pointer is over the card when it is over the place the card sits in, not
+ // over its turning outline, which narrows past a resting pointer and would send the card back and forth. A pointer that goes on
+ // or off the card takes over from a run.
+ let hovering=false;
+ function hoverFlip(on,trusted){
+ if(on===hovering)return;
+ hovering=on;
+ if(trusted)stopRun();
+ setFlipped(on);
+ }
+ function overCard(e,r){
+ return Math.abs(e.clientX-(r.left+r.width/2))<=card.offsetWidth/2&&Math.abs(e.clientY-(r.top+r.height/2))<=card.offsetHeight/2;
+ }
- // Cursor-driven idle 3D tilt via Pointer Events
- const reduce=matchMedia('(prefers-reduced-motion:reduce)');
- const canHover=matchMedia('(hover:hover)');
+ // Cursor-driven idle 3D tilt and Flips on hover, both via Pointer Events
stage.addEventListener('pointermove',e=>{
- if(reduce.matches||!canHover.matches||maxTilt===0)return;
+ if(e.pointerType==='touch')return;
const r=stage.getBoundingClientRect();
+ if(trigger==='hover')hoverFlip(overCard(e,r),e.isTrusted);
+ if(reduce.matches||!canHover.matches||maxTilt===0)return;
const x=(e.clientX-r.left)/r.width-.5;
const y=(e.clientY-r.top)/r.height-.5;
tilt.classList.add('tracking');
tilt.style.transform=`rotateX(${(-y*maxTilt).toFixed(2)}deg) rotateY(${(x*maxTilt).toFixed(2)}deg)`;
});
- stage.addEventListener('pointerleave',()=>{
+ stage.addEventListener('pointerleave',e=>{
+ if(trigger==='hover'&&e.pointerType!=='touch')hoverFlip(false,e.isTrusted);
tilt.classList.remove('tracking');
- tilt.style.transform='rotateX(0deg) rotateY(0deg)';
+ tilt.style.transform=FLAT;
});
- // Controls
- document.querySelectorAll('#axis button').forEach(b=>b.addEventListener('click',()=>{
- axis=b.dataset.axis;
- document.querySelectorAll('#axis button').forEach(x=>x.classList.toggle('on',x===b));
- card.classList.toggle('axis-x',axis==='x');
- }));
- document.querySelectorAll('#trigger button').forEach(b=>b.addEventListener('click',()=>{
- trigger=b.dataset.trig;
- document.querySelectorAll('#trigger button').forEach(x=>x.classList.toggle('on',x===b));
- if(trigger==='hover')setFlipped(false);
- }));
- document.getElementById('dur-sl').addEventListener('input',e=>{
- root.setProperty('--flip-dur',e.target.value+'ms');
- document.getElementById('dur-v').textContent=e.target.value+'ms';
- });
- document.getElementById('persp-sl').addEventListener('input',e=>{
- root.setProperty('--perspective',e.target.value+'px');
- document.getElementById('persp-v').textContent=e.target.value+'px';
- });
- document.getElementById('tilt-sl').addEventListener('input',e=>{
- maxTilt=+e.target.value;
- document.getElementById('tilt-v').textContent=maxTilt+'°';
- if(maxTilt===0)tilt.style.transform='rotateX(0deg) rotateY(0deg)';
+ 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('axis-seg',d=>{card.classList.toggle('axis-x',d.axis==='x');});
+ choices('speed-seg',d=>{flipMs=+d.ms;root.setProperty('--flip-dur',flipMs+'ms');});
+ choices('depth-seg',d=>{root.setProperty('--perspective',d.px+'px');});
+ choices('tilt-seg',d=>{maxTilt=+d.deg;if(maxTilt===0)tilt.style.transform=FLAT;});
+ document.getElementById('hover-tog').addEventListener('change',e=>{
+ trigger=e.target.checked?'hover':'click';
+ hovering=false;
+ if(e.target.checked)setFlipped(false);
});
</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 b9897568..5ff80c91 100644
--- a/index.html
+++ b/index.html
@@ -1025,7 +1025,7 @@ const CATS=[
['cloth-simulation','Cloth Simulation','A cloth sways in the wind, and you can drag it around. Best for playful pages.'],
['volumetric-smoke','Volumetric Smoke','Soft smoke curls upward, drawn as a real 3D cloud. Best for moody backgrounds.'],
['morphing-blob','Morphing Blob','Blobs melt together, and one drop chases your pointer. Best for hero sections.'],
- ['flip-card-3d','3D Flip Card','A card rotates in 3D to reveal its back face using preserve-3d, perspective, and backface-visibility.'],
+ ['flip-card-3d','3D Flip Card','A card turns over in 3D to show its back. Best for profile and product cards.'],
]
},
{