情境指针标签
Contextual Cursor Label
随指针移动的小标签会按目标切换动作文字,键盘聚焦时则贴住当前控件。
适用场景
适合沉浸式作品集或媒体索引里不同用途的操作区。 当外观看着相近的入口会触发不同操作时,可以用几种短动词区分。每个入口都应有可读的名称和可见的操作方式。
应用示例
摄影师的项目列表里,指针移到短片上时,跟随标签显示「播放」,移到相册上则显示「查看」。用键盘选中入口时,标签贴在焦点旁。
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;
}
.context-zone { position:relative; width:min(100%,280px); height:clamp(106px,68vh,174px); display:grid; align-items:center; grid-template-columns:repeat(3,1fr); gap:5px; padding:8px; background:#172a39; border:1px solid #82b3c070; border-radius:12px; }
.context-zone button { min-width:0; min-height:50px; padding:4px; border:1px solid #b4e3ec55; border-radius:8px; color:#edf9f8; background:#184d57; cursor:pointer; }
.context-zone button[aria-pressed="true"] { background:#286b6c; }
.context-label { position:absolute; z-index:2; pointer-events:none; left:0; top:0; color:#102b33; background:#dcfff6; padding:4px 7px; border-radius:20px; font-size:10px; font-weight:700; opacity:0; transition:opacity 120ms; }
.context-zone[data-visible] .context-label { opacity:1; }
@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">
<div class="lab context-zone">
<button type="button" data-context="0" aria-pressed="false">预览</button><button type="button" data-context="1" aria-pressed="false">试听</button><button type="button" data-context="2" aria-pressed="false">浏览</button>
<span class="context-label" aria-hidden="true"></span></div>
</main>
<script>
const zone = document.querySelector('.context-zone');
const cursorLabel = zone.querySelector('.context-label');
const controls = [...zone.querySelectorAll('button')];
function show(button, event) {
cursorLabel.textContent = button.textContent;
const bounds = zone.getBoundingClientRect();
const target = button.getBoundingClientRect();
const x = event ? event.clientX : target.left + 8;
const y = event ? event.clientY : target.bottom;
cursorLabel.style.left = Math.min(bounds.width - cursorLabel.offsetWidth - 4, Math.max(4,x - bounds.left + 8)) + 'px';
cursorLabel.style.top = Math.min(bounds.height - cursorLabel.offsetHeight - 4, Math.max(4,y - bounds.top + 8)) + 'px';
zone.dataset.visible = '';
}
controls.forEach((button) => {
button.addEventListener('pointermove', (event) => { if (event.pointerType !== 'touch' && !matchMedia('(prefers-reduced-motion: reduce)').matches) show(button,event); });
button.addEventListener('focus', () => show(button));
button.addEventListener('click', () => {
controls.forEach((item) => item.setAttribute('aria-pressed', String(item === button)));
show(button);
});
});
zone.addEventListener('pointerleave', () => { if (!zone.matches(':focus-within')) delete zone.dataset.visible; });
zone.addEventListener('focusout', (event) => { if (!zone.contains(event.relatedTarget)) delete zone.dataset.visible; });
</script>
</body>
</html>UI Skills 原创示例