悬停图像切片错位
Hover Image Slices
原本对齐的竖向切片交替向上或向下移动,形成错位画面。
适用场景
为封面缩略图或内容专题加入图形化动效。 竖向切片错开后,主体也应容易辨认。标题放在图外,避免随着切片断开。
应用示例
建筑特辑里有一张条纹音乐厅的缩略图。指针进入时,画面中的竖条交错上下移动,移开后大楼的轮廓重新对齐。
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;
}
.scene-art {
position: absolute; inset: 0;
background:
radial-gradient(circle at 68% 33%, var(--sun, #ffdbac) 0 11%, transparent 11.8%),
linear-gradient(26deg, var(--land, #435b77) 0 28%, transparent 28.6%),
linear-gradient(-33deg, var(--ridge, #736ca9) 0 32%, transparent 32.5%),
linear-gradient(145deg, var(--horizon, #151b48), var(--sky, #7c729a));
}
.scene-art::after {
position: absolute; inset: 0; content: '';
background-image: repeating-linear-gradient(0deg, #fff0 0 10px, #ccceff20 11px, #fff0 12px);
mask-image: linear-gradient(transparent 45%, black 100%);
}
.image-slices { position:absolute; inset:5px; border-radius:8px; overflow:hidden; isolation:isolate; }
.image-slices i { clip-path:inset(0 calc((6 - var(--section)) * (100% / 7)) 0 calc(var(--section) * (100% / 7))); transition:transform 390ms cubic-bezier(.22,.8,.24,1) calc(var(--section) * 18ms); }
.pointer-stage:is(:hover, :focus-visible, [aria-pressed="true"]) .image-slices i { transform:translateY(calc(var(--offset) * var(--distance))); }
@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="image-slices" aria-hidden="true"><i class="scene-art" style="--section:0;--offset:-1;--distance:9px"></i><i class="scene-art" style="--section:1;--offset:1;--distance:11px"></i><i class="scene-art" style="--section:2;--offset:-1;--distance:13px"></i><i class="scene-art" style="--section:3;--offset:1;--distance:15px"></i><i class="scene-art" style="--section:4;--offset:-1;--distance:17px"></i><i class="scene-art" style="--section:5;--offset:1;--distance:19px"></i><i class="scene-art" style="--section:6;--offset:-1;--distance:10px"></i></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 原创示例