指针多层视差
Layered Pointer Parallax
指针移动时近景的位移大于远景,平面画面因此呈现层次。
适用场景
为插画、产品场景或首屏氛围增加空间层次。 画面先分出清楚的远近层次,较远图层只做很小的位移。细字和按钮放在稳定的图层上。
应用示例
水族馆首页的海底插画里,访客移动指针时,近处的海藻移动多一些,远处的珊瑚礁只轻轻偏移。展览标题始终在原位。
AI 提示词
给【目标位置】添加指针多层视差。将插画拆成可独立合成的画层,用同一个归一化移动向量乘以不同的景深系数。为画层预留超出画框的边缘。保留键盘操作,并提供减少动态效果时的状态。
示例代码
保存为 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;
}
.pointer-stage {
--x: 50%; --y: 50%; --dx: 0; --dy: 0;
display: grid; place-items: center; position: relative;
width: min(100%, 280px); height: clamp(106px, 68vh, 174px);
overflow: hidden; isolation: isolate;
border: 1px solid #b8a8ff70; border-radius: 13px;
background: #23213e; color: #fcf9ff; cursor: pointer;
touch-action: manipulation;
}
.pointer-stage:focus-visible { outline-offset: 3px; }
.pointer-stage .tag {
position: absolute; z-index: 4; bottom: 7px; left: 8px; color: #fff;
background: #17152fe8; border-radius: 5px; padding: 3px 7px;
font-size: 10px; line-height: 1.4;
}
.depth-sky { position: absolute; inset: -10px; background: radial-gradient(circle at 10% 20%, #d3f0ff 0 1px, transparent 2px) 0 0 / 16px 15px, linear-gradient(155deg,#182446,#6b537c); }
.depth-moon { position: absolute; top: 21%; left: 55%; width: 39px; aspect-ratio: 1; border-radius: 50%; background: #ffe7b9; box-shadow: 0 0 18px #ffe7b955; transform: translate(calc(var(--dx)*4px),calc(var(--dy)*3px)); }
.depth-mountains, .depth-valley { position: absolute; inset: 22% -14px -14px; }
.depth-mountains { clip-path: polygon(0 59%,23% 10%,39% 60%,62% 18%,83% 66%,100% 27%,100% 100%,0 100%); background: linear-gradient(145deg,#7e8dcc,#455482); transform: translate(calc(var(--dx)*9px),calc(var(--dy)*5px)); }
.depth-valley { clip-path: polygon(0 65%,27% 50%,47% 75%,72% 50%,100% 65%,100% 100%,0 100%); background: linear-gradient(125deg,#6bd7c5,#194954); transform: translate(calc(var(--dx)*15px),calc(var(--dy)*9px)); }
@media (prefers-reduced-motion: reduce) {
.lab, .lab *, .lab::before, .lab::after, .lab *::before, .lab *::after {
animation: none !important;
transition: none !important;
scroll-behavior: auto !important;
}
}
</style>
</head>
<body>
<main class="demo">
<button type="button" aria-label="移动景深画层" aria-pressed="false" class="lab pointer-stage">
<span class="depth-sky" aria-hidden="true"></span>
<span class="depth-moon" aria-hidden="true"></span>
<span class="depth-mountains" aria-hidden="true"></span>
<span class="depth-valley" aria-hidden="true"></span>
<span class="tag" aria-hidden="true">三层景深</span></button>
</main>
<script>
const stage = document.querySelector('.pointer-stage');
const reduced = matchMedia('(prefers-reduced-motion: reduce)');
const clamp = (value, min, max) => Math.max(min, Math.min(max, value));
function setPosition(x, y) {
stage.style.setProperty('--x', (x * 100) + '%');
stage.style.setProperty('--y', (y * 100) + '%');
stage.style.setProperty('--dx', 2 * x - 1);
stage.style.setProperty('--dy', 2 * y - 1);
}
function position(event) {
const bounds = stage.getBoundingClientRect();
return [clamp((event.clientX - bounds.left) / bounds.width, 0, 1), clamp((event.clientY - bounds.top) / bounds.height, 0, 1)];
}
stage.addEventListener('pointermove', (event) => {
if (event.pointerType !== 'touch' && !reduced.matches) setPosition(...position(event));
});
stage.addEventListener('pointerleave', () => {
if (stage.getAttribute('aria-pressed') !== 'true') setPosition(.5, .5);
});
stage.addEventListener('click', (event) => {
const active = stage.getAttribute('aria-pressed') !== 'true';
stage.setAttribute('aria-pressed', String(active));
setPosition(...(active && event.detail ? position(event) : active ? [.68, .38] : [.5, .5]));
});
</script>
</body>
</html>UI Skills 原创示例