Shared Element Transition
共享元素转场
A thumbnail visibly travels and grows into the detail view, then retraces the same path when you go back.
Where to use it
Keep the selected work identifiable while moving between a gallery, product list, or portfolio and its detail view. Choose a distinctive asset that appears in both views and keep that asset visible through the layout change.
Practical example
A furniture showroom shows an armchair photo in the grid. Opening it grows the same photo into the detail cover, and returning sends the photo back to the chair’s original card.
AI prompt
Add a Shared Element Transition from [source thumbnail] to [detail cover]. Match the same artwork, measure its visible rectangle before and after the layout change, and use FLIP with `Element.animate()` to interpolate translation and scale. Reverse the mapping on close, restore keyboard focus to the original control, and use an immediate state change for reduced motion.
Example code
Save the standalone HTML as a file and open it in your browser. It uses the same code as the preview.
View HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Shared Element Transition</title>
<style>
:root {
color-scheme: dark;
--demo-bg: #171629;
--demo-ink: #f8f5ff;
--demo-accent: #b9a6ff;
--demo-muted: #bfbbd3;
font-family: system-ui, sans-serif;
color: var(--demo-ink);
background: var(--demo-bg);
}
*, *::before, *::after { box-sizing: border-box; }
body { margin: 0; min-width: 0; }
button, input { font: inherit; }
.demo {
display: grid;
place-items: center;
align-content: center;
min-height: 100vh;
padding: 14px;
overflow: hidden;
background: radial-gradient(ellipse at 50% 24%, #28233f, var(--demo-bg) 78%);
}
.demo-button {
border: 1px solid #ffffff3d;
border-radius: 7px;
background: #ffffff12;
color: var(--demo-ink);
padding: 6px 11px;
font-size: 12px;
cursor: pointer;
}
:focus-visible {
outline: 2px solid var(--demo-accent);
outline-offset: 3px;
}
.shared-scene {
width: min(100%, 302px); height: 145px; padding: 11px;
position: relative; isolation: isolate; border: 1px solid #67557d;
border-radius: 15px; background: #21192f; overflow: hidden;
}
.shared-heading { margin: 0 0 8px; font-size: 12px; font-weight: 680; color: #e1d2ed; }
.shared-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.shared-tile {
display: grid; min-width: 0; gap: 5px; align-content: start; text-align: left;
border: 1px solid #654968; border-radius: 8px; background: #352440;
color: #fff3fc; padding: 5px; font-size: 10px;
}
button.shared-tile { cursor: pointer; background: #482b46; border-color: #a3758e; }
button.shared-tile:hover { border-color: #f6b8ba; }
.shared-origin, .shared-ridge { display: block; height: 61px; min-width: 0; }
.shared-artwork {
display: block; width: 100%; height: 100%; border-radius: 7px;
position: relative; transform-origin: top left; overflow: hidden;
background:
radial-gradient(ellipse at 76% 28%, #ffe6a9 0 10%, transparent 11%),
radial-gradient(ellipse at 10% 131%, #814281 0 51%, transparent 52%),
radial-gradient(ellipse at 67% 147%, #bd5e83 0 57%, transparent 58%),
radial-gradient(ellipse at 98% 145%, #f59b87 0 59%, transparent 60%),
linear-gradient(156deg, #442049 11%, #9a547b 48%, #fac096 97%);
}
.shared-ridge {
border-radius: 7px;
background:
radial-gradient(ellipse at 27% 23%, #eed2ab 0 8%, transparent 9%),
repeating-linear-gradient(140deg, transparent 0 12px, #e2add47a 13px 14px),
linear-gradient(160deg, #795477, #30233e);
}
.shared-title { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.shared-detail-bar { display: flex; align-items: center; gap: 8px; margin-bottom: 7px; }
.shared-detail-bar h2 { min-width: 0; margin: 0; line-height: 1.2; font-size: 11px; }
.shared-back {
border-radius: 5px; border: 1px solid #997599; background: #3e2c4b;
color: #f9e5f5; flex: none; padding: 4px 7px; cursor: pointer; font-size: 10px;
}
.shared-destination { height: 86px; }
</style>
</head>
<body>
<main class="demo">
<div class="shared-scene" data-open="false">
<div class="shared-library">
<p class="shared-heading">Visual studies</p>
<div class="shared-grid">
<button class="shared-tile" type="button" aria-controls="shared-detail" aria-expanded="false" data-testid="shared-element-open">
<span class="shared-origin"><span class="shared-artwork" aria-hidden="true" data-testid="shared-element-artwork"></span></span>
<span class="shared-title">Dune studies</span>
</button>
<div class="shared-tile shared-other">
<span class="shared-ridge" aria-hidden="true"></span>
<span class="shared-title">Ridge lines</span>
</div>
</div>
</div>
<section id="shared-detail" class="shared-detail" aria-labelledby="shared-title" data-testid="shared-element-detail" hidden>
<div class="shared-detail-bar">
<button class="shared-back" type="button" data-testid="shared-element-close">← Back</button>
<h2 id="shared-title">Dune studies</h2>
</div>
<div class="shared-destination"></div>
</section>
</div>
</main>
<script>
const scene = document.querySelector('.shared-scene');
const library = scene.querySelector('.shared-library');
const detail = scene.querySelector('.shared-detail');
const open = scene.querySelector('[data-testid="shared-element-open"]');
const back = scene.querySelector('[data-testid="shared-element-close"]');
const artwork = scene.querySelector('.shared-artwork');
const origin = scene.querySelector('.shared-origin');
const destination = scene.querySelector('.shared-destination');
const reduced = matchMedia('(prefers-reduced-motion: reduce)');
let animation;
function showDetail(isOpen) {
if (scene.dataset.open === String(isOpen)) return;
// First measure the currently visible bounds, including any interrupted motion.
const first = artwork.getBoundingClientRect();
animation?.cancel();
(isOpen ? destination : origin).append(artwork);
detail.hidden = !isOpen;
library.hidden = isOpen;
scene.dataset.open = String(isOpen);
open.setAttribute('aria-expanded', String(isOpen));
(isOpen ? back : open).focus({ preventScroll: true });
// Last measure the destination, then invert its transform back to the first box.
if (reduced.matches || typeof artwork.animate !== 'function') return;
const last = artwork.getBoundingClientRect();
if (!last.width || !last.height) return;
const x = first.left - last.left;
const y = first.top - last.top;
const scaleX = first.width / last.width;
const scaleY = first.height / last.height;
animation = artwork.animate([
{ transform: 'translate(' + x + 'px, ' + y + 'px) scale(' + scaleX + ', ' + scaleY + ')' },
{ transform: 'none' },
], { duration: 490, easing: 'cubic-bezier(.2, .75, .24, 1)' });
}
open.addEventListener('click', () => showDetail(true));
back.addEventListener('click', () => showDetail(false));
scene.addEventListener('keydown', (event) => {
if (event.key === 'Escape') showDetail(false);
});
reduced.addEventListener('change', () => animation?.cancel());
</script>
</body>
</html>Original example by UI Skills