悬停显示操作
Hover Reveal Actions
悬停某一行或把焦点移入该行时,显示对应操作。
适用场景
适合文件列表和项目管理器中的辅助操作。 适合把偶尔用到的操作收在列表行尾。行内有键盘焦点时也要显示这些按钮,触屏应提供可见的操作入口。
应用示例
在播客后台的单集列表,鼠标移到某一集时,行尾淡入三点菜单。编辑可就地重命名或归档该集,然后继续浏览列表。
AI 提示词
为【目标位置】添加 Hover Reveal Actions(悬停显示操作)。把操作保留在 DOM 中,用悬停和 :focus-within 显示。预留控件空间以避免标题移动,并在触屏设备上直接显示操作。在减少动态效果模式下保持内容可读,并保留键盘操作。
示例代码
保存为 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;
}
.file { display: flex; gap: 7px; align-items: center; width: min(100%, 215px); padding: 10px; border: 1px solid #77639a; border-radius: 9px; background: #302446; }
.symbol { color: #ccb9ff; }
.filename { flex: 1; font-size: 13px; }
.file button { min-width: 51px; transition: opacity .18s ease; }
.file button[aria-pressed="true"] { background: #665098; }
@media (hover: hover) and (pointer: fine) {
.file:not(:hover):not(:focus-within) button[aria-pressed="false"] { opacity: 0; }
}
@media (prefers-reduced-motion: reduce) { .file button { transition: none; } }
</style>
</head>
<body>
<main class="demo">
<div class="file">
<span class="symbol" aria-hidden="true">▧</span>
<span class="filename">品牌笔记</span>
<button type="button" class="demo-button" aria-pressed="false">置顶</button>
</div>
</main>
<script>
const button = document.querySelector('.file button');
button.addEventListener('click', () => {
button.setAttribute('aria-pressed', String(button.getAttribute('aria-pressed') !== 'true'));
});
</script>
</body>
</html>UI Skills 原创示例