指针十字准线
Pointer Crosshair
一横一竖两条定位线跟随指针移动,在指针处交叉。
适用场景
适合图表读数或编辑器画布定位。 需要精确读值时,可以让十字线搭配数据值或坐标一起显示,用户更容易对应到坐标轴。
应用示例
天气看板的逐小时气温图上,横竖参考线跟着指针移动。用户对准某个点,就能从旁边的读数看到时间和气温。
AI 提示词
为【目标位置】添加 Pointer Crosshair(指针十字准线)。保留原生光标与焦点指示。用局部指针坐标定位横纵引导线,并在聚焦时显示居中的参考线。在减少动态效果模式下保持内容可读,并保留键盘操作。
示例代码
保存为 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;
}
}
.target { min-height: 112px; overflow: hidden; background-size: 17px 17px;
background-image: linear-gradient(#8e79c225 1px,transparent 1px),linear-gradient(90deg,#8e79c225 1px,transparent 1px); }
.target::before, .target::after {
position: absolute; content: ''; pointer-events: none; background: #cdb6ffbb; opacity: 0;
}
.target::before { left: var(--x,50%); inset-block: 0; width: 1px; }
.target::after { top: var(--y,50%); inset-inline: 0; height: 1px; }
.target:is(:hover, :focus-visible, [aria-pressed="true"])::before, .target:is(:hover, :focus-visible, [aria-pressed="true"])::after { opacity: 1; }
.target span { z-index: 1; padding: 4px 7px; border-radius: 4px; background: #251d37; font-size: 12px; }
</style>
</head>
<body>
<main class="demo">
<button class="target" type="button" aria-pressed="false" aria-label="十字准线"><span>十字准线</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 原创示例