级联菜单的经典问题是:鼠标从父菜单斜着移向子菜单,途中会擦过下面的几行。普通菜单一碰到新行就切换,原来的子菜单随即消失,用户只能重新开始。

这不是动画不够顺滑,而是命中逻辑把“经过”误判成了“选择”。

安全三角区:判断鼠标是不是在赶来

解决办法是建立一个临时安全区。以鼠标上一刻的位置为顶点,连接子菜单靠近父菜单一侧的上下两个角,围成一个三角形。鼠标位于三角形内时,说明它大概率正在朝子菜单移动,沿途经过的菜单行都应被忽略。

这里有两个容易踩坑的细节:

  • 顶点必须用稍早的位置。如果直接用当前指针位置作顶点,三角形会退化成一条线,几乎失去判定意义。
  • 安全区不能无限延伸。可以只保护父菜单朝向子菜单的最后一段区域,例如宽度约为父菜单的 43%,并限制在 56px 到 90px 之间。超过这个范围,应恢复正常的行切换。

安全区数学最好抽成纯函数,用三个边向量的叉积符号判断点是否在三角形内,并配套单元测试。把它埋在组件事件里,后续调整布局时很容易改坏。

三个延时,各管一件事

稳定感不能只靠一个“关闭延时”硬撑。打开、保护和关闭是三种不同意图,应分别处理:

  • 打开延时:220ms。避免快速掠过菜单时误开子菜单。
  • 安全窗口:1200ms。指针停在保护区内时,仍能在超时后切换。
  • 关闭延时:360ms。短暂滑出菜单时不立即收起。

安全窗口需要周期性复查,例如每 80ms 检查一次。指针离开安全区就立即恢复正常切换;如果指针一直停在安全区内,则在 1200ms 后切换,避免用户“停在那里却什么也打不开”。

拖拽场景还要另算:拖动项目经过文件夹行时,展开延时可以放宽到 420ms。拖拽本身需要更多容错,不能用悬停的节奏。

另外,行的悬停高亮必须由组件状态驱动,而不是 CSS 的 :hover。否则即使逻辑上已经跳过某一行,视觉上它仍会亮起,用户会以为菜单选中了错误目标。

方向、尺寸与动效要一起设计

子菜单默认向右展开,并与父级重叠约 8px,让第一行与父行对齐。空间不足时翻转到左侧,安全三角区也要同步改用子菜单的另一侧边缘。

每级菜单的宽度可以在 210px 到 260px 之间按内容自适应,最大高度受视口限制并独立滚动。玻璃质感来自半透明表面、细边框、背景模糊和克制的内外阴影,而不是大面积高光和强投影。

入场动效可以带一点轻微回弹:从不透明度和位移开始,透明度变化快于位移,保留轻微的 overshoot。回弹是“菜单从父级边缘弹出”的物理暗示;如果没有这层含义,只是为了让动画显得活泼,就不必加。

文件夹图标从合到开的变化也应与菜单状态一致。若两个图标的路径结构兼容,可以做形变;否则让前面板轻微抬起并交叉淡入淡出。两者都比生硬替换更自然。

键盘与无障碍不是附加项

鼠标优化不能以牺牲键盘操作换取。一个完整的级联菜单至少要支持:

  • 方向键在当前层级移动;
  • Right 或 Enter 展开文件夹;
  • Left 返回上一级;
  • Escape 关闭全部菜单,并把焦点还给触发按钮;
  • 文件夹行使用 aria-expanded 表达展开状态;
  • 使用 menu 与 menuitem 角色,让辅助技术理解结构。

如果项目已有菜单组件,应把安全区逻辑接入它现有的子菜单行为,而不是另起一个平行实现。这样键盘支持、焦点管理和无障碍语义才不会在“优化手感”的过程中被丢掉。

级联菜单示例 Prompt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
Define a cascading (nested) menu component that keeps its submenus steady:
a safe zone plus three delays, so submenus open on purpose, survive diagonal
moves toward them, and do not vanish on a brief slip out of the menu.
If the project already has a menu or dropdown component, build this into its
existing submenu logic and keep its API, keyboard support and accessibility
as they are. Otherwise create one reusable CascadeMenu:

Props: an anchor (the trigger button that opens it on click) and a tree of
items, each { label, icon?, href?, children? }. An item with children is a
folder row; one without is a plain row that navigates on click.
Each level shows its folder's name as a small title above its rows.
Keyboard: arrow keys move through rows, Right/Enter opens a folder, Left goes
back a level, Escape closes everything and returns focus to the anchor.
Use menu / menuitem roles and aria-expanded on folder rows.
Behavior, with the values to use:

Open delay: hovering a row schedules it to take effect after 220ms: a folder
row opens its submenu, a plain row closes any deeper submenu. Leaving the row
before then cancels it. Touch input acts at once.
Safe zone: while a submenu is open, build a triangle from the pointer's
previous position to the two corners of the submenu's near edge (the left
edge's top and bottom for a submenu on the right). Track the last two
pointermove positions and use the older one as the apex, so the triangle
never collapses onto the current point. If the pointer is inside that
triangle and within the parent's last min(90px, max(56px, 43% of the parent
width)) toward the submenu, it is on its way there: rows it crosses must not
highlight or open their own submenus.
Safe window: a row hovered inside the safe zone does not switch the submenu
right away. Recheck every 80ms; switch once the pointer leaves the zone, or
when 1200ms have passed since the hover began, so a pointer that simply stops
there still gets its new submenu.
Close delay: when the pointer leaves the anchor, every open level and the
safe zone, close the whole menu after 360ms. Coming back in time cancels the
close. Clicking outside closes at once.
Dragging an item over a folder row opens it after 420ms instead of 220ms.
The pointer inside an open submenu ends all protection.
Drive row highlight from component state (hovered, active, skipped), not
from CSS :hover, or rows crossed inside the safe zone will still light up.
Geometry and motion:

Submenus open to the right, overlap their parent by 8px and align their first
row with the parent row. Flip to the left when there is no room; the safe zone
then uses the submenu's right edge.
Levels: 210-260px wide, sized to content, max height min(420px, 100vh - 24px)
with their own scroll, 6px padding, 12px radius, 1px border, backdrop-filter
blur(18px) saturate(135%).
Rows: 32px tall, 8px radius, 4px apart, 0 8px padding, 8px gap; 18px icon,
14px weight-500 label, a 16px chevron on folder rows. Level title: 11px,
weight 600, quiet ink, padding 4px 8px 2px.
A submenu opens from its parent's edge: from translateX(-8px) scale(0.88, 0.98)
and opacity 0, with opacity over 170ms and transform over 360ms on
cubic-bezier(0.2, 1.45, 0.35, 1).
The root level opens the same way from its anchor: from translateY(-6px)
scale(0.96, 0.86), top-left origin. Keep the overshoot curve: the slight
spring is part of the feel.
A folder row's icon turns from a closed folder into an open one while its
submenu is open, and back when it closes, over 460ms on
cubic-bezier(0.22, 1, 0.36, 1). Morph the paths if the two glyphs share their
structure; otherwise cross-fade them with a slight lift of the front panel.
Light and dark themes come from tokens, not separate components:

Light: surface rgba(255,255,255,.96), border rgba(0,0,0,.14), row ink #263140,
row hover rgba(15,23,42,.07), quiet ink #7A8491, chevron #94A3B8,
shadow 0 18px 34px rgba(15,23,42,.12), 0 2px 4px rgba(15,23,42,.05),
inset 0 1px 0 rgba(255,255,255,.74).
Dark, neutral grays with no blue cast: surface rgba(34,34,34,.96),
border rgba(255,255,255,.12), row ink #E4E4E4, row hover rgba(255,255,255,.08),
quiet ink #9A9A9A, chevron #8A8A8A, shadow 0 18px 34px rgba(0,0,0,.42),
0 2px 4px rgba(0,0,0,.2), inset 0 1px 0 rgba(255,255,255,.06).
Monochrome brand icons invert in dark. Switching themes cross-fades colors,
borders and shadows over about 500ms.
Keep every timing value as a named constant in one place, and keep the
safe-zone math in a pure, unit-tested helper (point-in-triangle by the sign of
the three edge cross products). Add an optional debug overlay that draws the
zone (blue fill at 16%, dashed blue outline) and the running timer, off by
default.

质感来自可解释的细节

级联菜单的安全区解释了“它为什么知道我要去哪里”,分层延时则解释了“它为什么不会突然消失”。用户说不出具体参数,却能立刻感觉到产品是否可靠。

对 AI 生成界面而言,布局和配色已经越来越容易及格,真正拉开差距的,是这些能被逻辑解释、能被工程复用、也能被测试覆盖的交互约束。

站内搜索

没有找到内容!