Contextual Cursor Label
情境指针标签
A small following label changes its action verb over each target and snaps to the focused control for keyboard input.
Where to use it
Clarify different actions inside an immersive portfolio or media index. Use a small set of distinct verbs when visually similar targets do different things. Keep accessible names and visible controls for each target.
Practical example
In a photographer's project index, the following label says Play over a moving image and View over a still gallery. Tabbing to a target places the label beside its focused control.
AI prompt
Add Contextual Cursor Label to [target]. Keep each control’s real accessible label. Update a decorative floating badge from the active target, clamp its position inside the surface, and pin it to focused controls for keyboard users. Keep the controls keyboard accessible and provide a reduced-motion state.
Example code
Save the standalone HTML as a file and open it in your browser. It uses the same code as the preview.
View HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Contextual Cursor Label</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">View</button><button type="button" data-context="1" aria-pressed="false">Listen</button><button type="button" data-context="2" aria-pressed="false">Browse</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>Original example by UI Skills