指针跟随图片预览
Cursor Image Preview
悬停作品标题时,缩略图出现在指针旁并跟随移动。
适用场景
适合用简洁文字列表浏览作品。 将浮动预览放在文字列的空白侧,避免盖住其他标题。键盘聚焦时也可以在标题旁固定显示预览。
应用示例
摄影档案用标题列出相册。指针移到「清晨的港口」这一行时,旁边浮出一张小图,让读者点开相册前就能辨认画面。
AI 提示词
为【目标位置】添加 Cursor Image Preview(指针跟随图片预览)。在指针旁显示不拦截操作的缩略图,并限制在可视区域内。使用键盘或轻触时,在固定位置显示图片。在减少动态效果模式下保持内容可读,并保留键盘操作。
示例代码
保存为 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;
}
.target {
position: relative;
display: grid;
place-items: center;
width: min(100%, 208px);
min-height: 84px;
padding: 10px;
color: #f5efff;
border: 1px solid #aa91ee80;
border-radius: 13px;
background: #29213f;
cursor: pointer;
font: 680 15px system-ui, sans-serif;
}
.target[aria-pressed="true"] { outline: 2px solid #d9c9ff; outline-offset: 3px; }
@media (prefers-reduced-motion: reduce) {
.target, .target *, .target::before, .target::after, .target *::before, .target *::after {
transition: none !important;
animation: none !important;
}
}
.picture {
width: 100%; min-height: 106px; border-radius: 9px;
background:
radial-gradient(circle at 72% 29%, #ffe6af 0 12px, #ffd9a844 13px 18px, transparent 19px),
linear-gradient(158deg, transparent 53%, #6652a6 53.5% 72%, transparent 72.5%),
linear-gradient(45deg, #453676 26%, #705ab1 27% 45%, transparent 45.5%),
linear-gradient(150deg, #574180, #a06fa6 63%, #da929a);
transition: transform .35s ease, filter .35s ease, opacity .28s ease;
}
.target { min-height: 117px; display: flex; justify-content: start; overflow: hidden; }
.target strong { z-index: 1; font-size: 13px; padding: 4px; background: #29213f; }
.target .picture {
position: absolute; pointer-events: none; width: 70px; min-height: 48px;
border: 1px solid #e3d6fb88; left: clamp(64px, var(--x, 58%), calc(100% - 76px));
top: clamp(4px, var(--y, 26%), calc(100% - 54px)); opacity: 0;
}
.target:is(:hover, :focus-visible, [aria-pressed="true"]) .picture { opacity: 1; }
</style>
</head>
<body>
<main class="demo">
<button class="target" type="button" aria-pressed="false" aria-label="庭园项目"><strong>庭园项目</strong><span class="picture" aria-hidden="true"></span></button>
</main>
<script>
const target = document.querySelector('.target');
target.addEventListener('click', () => {
target.setAttribute('aria-pressed', String(target.getAttribute('aria-pressed') !== 'true'));
});
const zone = document.querySelector('.target');
const reduce = matchMedia('(prefers-reduced-motion: reduce)');
function update(event) {
if (reduce.matches) return;
const rect = zone.getBoundingClientRect();
const x = Math.min(rect.width, Math.max(0, event.clientX - rect.left));
const y = Math.min(rect.height, Math.max(0, event.clientY - rect.top));
zone.style.setProperty('--x', x + 'px');
zone.style.setProperty('--y', y + 'px');
zone.style.setProperty('--dx', ((x / rect.width - .5) * 2).toFixed(3));
zone.style.setProperty('--dy', ((y / rect.height - .5) * 2).toFixed(3));
}
zone.addEventListener('pointermove', update);
zone.addEventListener('pointerdown', update);
zone.addEventListener('pointerleave', () => {
if (zone.getAttribute('aria-pressed') === 'true') return;
for (const property of ['--x', '--y', '--dx', '--dy']) zone.style.removeProperty(property);
});
</script>
</body>
</html>UI Skills 原创示例