鼠标聚光
Cursor Spotlight
柔和的光斑在区域内跟随指针移动,指针离开后淡出。
适用场景
突出功能卡片或深色首屏区块,同时保持文字清晰可读。 光斑适合放在深色卡片的底层,亮度压低,并且限制在卡片边界内。
应用示例
相片编辑工具的功能介绍区可以在「降噪」卡片上加一束冷色光。用户移动指针时,光斑跟着移动,标题和图标仍留在原位。
也叫
- 指针聚光
AI 提示词
在【目标区域】加上 Cursor Spotlight。用区域内的指针坐标定位低透明度的径向渐变,光晕裁在边缘内,移出时淡出。让光晕位于内容下方并允许指针事件传到控件。触屏与减少动态效果模式下用固定高光。
示例代码
保存为 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;
}
.spotlight {
position: relative;
isolation: isolate;
display: grid;
align-content: center;
justify-items: center;
gap: 6px;
width: min(100%, 242px);
min-height: 108px;
padding: 12px;
overflow: hidden;
color: #eee6ff;
background:
linear-gradient(90deg, #c4afff1c 1px, transparent 1px),
linear-gradient(#c4afff1c 1px, transparent 1px),
#241c3a;
background-size: 18px 18px;
border: 1px solid #645484;
border-radius: 13px;
font: inherit;
cursor: pointer;
}
.spotlight::before {
position: absolute;
z-index: -1;
inset: 0;
opacity: 0.48;
background: radial-gradient(100px circle at var(--light-x, 50%) var(--light-y, 50%), #c79dffb5, transparent 95%);
content: "";
transition: opacity 180ms ease;
}
.spotlight:is(:focus-visible, [aria-pressed="true"])::before { opacity: 1; }
@media (hover: hover) { .spotlight:hover::before { opacity: 1; } }
.spotlight-mark { color: #fff3d0; font-size: 26px; line-height: 1; }
.spotlight strong { font-size: 12px; }
@media (prefers-reduced-motion: reduce) {
.spotlight::before { transition: none; }
}
</style>
</head>
<body>
<main class="demo">
<button class="spotlight" type="button" aria-pressed="false">
<span class="spotlight-mark" aria-hidden="true">✦</span>
<strong>光影</strong>
</button>
</main>
<script>
const card = document.querySelector(".spotlight");
const reduceMotion = window.matchMedia("(prefers-reduced-motion: reduce)");
card.addEventListener("pointermove", (event) => {
if (event.pointerType === "touch" || reduceMotion.matches) return;
const { left, top } = card.getBoundingClientRect();
card.style.setProperty("--light-x", (event.clientX - left) + "px");
card.style.setProperty("--light-y", (event.clientY - top) + "px");
});
function resetSpotlight() {
card.style.removeProperty("--light-x");
card.style.removeProperty("--light-y");
}
card.addEventListener("pointerleave", resetSpotlight);
card.addEventListener("pointercancel", resetSpotlight);
reduceMotion.addEventListener("change", resetSpotlight);
card.addEventListener("click", () => {
const lit = card.getAttribute("aria-pressed") === "true";
card.setAttribute("aria-pressed", String(!lit));
});
</script>
</body>
</html>UI Skills 原创示例