共享元素转场
Shared Element Transition
缩略图移动并放大成详情封面,返回时沿原路径缩回列表。
适用场景
用于图片图库、商品列表或作品集到详情页的切换,让用户一直看得见自己选中的内容。 选一张两种视图里都会出现且容易辨认的图,让它贯穿整个版面切换。
应用示例
家具展厅的网格中有一张扶手椅照片。打开详情时,同一张照片从卡片里放大为详情封面,返回后再缩回原来那张卡片。
AI 提示词
让【列表缩略图】平滑过渡到【详情封面】。匹配同一张图,切换布局前后读取可见矩形,用 FLIP 和 Element.animate() 补间位移与缩放。关闭时反向映射,将键盘焦点还给原控件,减少动态效果模式直接切换状态。
示例代码
保存为 HTML 文件后,用浏览器打开即可运行。代码与页面里的演示一致。
查看 HTML
<!doctype html>
<html lang="zh">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>共享元素转场</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">平面作品</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">沙丘色彩研究</span>
</button>
<div class="shared-tile shared-other">
<span class="shared-ridge" aria-hidden="true"></span>
<span class="shared-title">山脊轮廓</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">← 返回</button>
<h2 id="shared-title">沙丘色彩研究</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>UI Skills 原创示例