YL

FORM & INPUT

开关Toggle Switch

一颗滑块在轨道里左右滑动,开是绿色关是灰色——比复选框更"物理"的布尔选择。

中英对照

中文
开关
英文
Toggle Switch
别名
切换开关、iOS Switch、拨动开关
一句话
一颗滑块在轨道里左右滑动,开是绿色关是灰色——比复选框更"物理"的布尔选择。

效果预览

邮件通知
营销推送
自动保存

适合用在哪

设置页、通知偏好、任何开/关语义的选项。

不适合:需要点「保存」才生效的偏好:开关语义是立即生效;多选项互斥也不要用一排开关,改用单选。

和相近模式的区别

自定义勾选框Custom Checkbox & Radio

复选框适合「待保存的同意项/多选」;开关适合设置页里立刻生效的布尔项。

文案切换按钮Label Swap Button

交换按钮是点击后文案/图标对换的微交互;开关是轨道滑块的物理隐喻,有开/关两端。

容易踩的坑

  • 需要点「保存」才生效的设置也用开关,用户以为已生效直接离开导致设置丢失。
  • 开关状态只靠绿色和灰色区分,色弱用户无法判断开关,应加位置或图标提示。
  • 用 div 模拟却没加 role="switch" 和 aria-checked,键盘无法切换、读屏不播报状态。

设计提示词 · Prompt

开关(Toggle Switch):轨道 44-52×26-30px、圆角 999px;滑块比轨道矮 4px、左右留 2px、带 1px 投影。开态轨道变绿色(或品牌色),滑块 translateX 滑到右侧(220ms cubic-bezier(.32,.72,0,1))。语义必须严格:开关只用于"立即生效"的布尔设置;需要按"保存"才生效的选项改用复选框。整行布局:左侧标签 + 说明文字,右侧开关,行间用分隔线。悬停时轨道轻微加深暗示可点。
Toggle Switch: track 44-52×26-30px, border radius 999px; the thumb is 4px shorter than the track, with 2px left/right inset and a 1px drop shadow. In the on state the track turns green (or brand color) and the thumb slides right via translateX (220ms cubic-bezier(.32,.72,0,1)). Semantics must be strict: switches are only for boolean settings that "take effect immediately"; options that require pressing "Save" should use a checkbox instead. Row layout: label + description on the left, switch on the right, rows separated by dividers. On hover the track darkens slightly to hint it is clickable.

可复制 CSS

自包含的最小实现,粘贴到空白页面即可生效;类名可以按项目规范改名。

/* 轨道约 48×28;滑块 translateX */
.toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px 0;
  border-bottom: 1px solid #e5e7eb;
  font: 15px/1.4 system-ui, sans-serif;
  color: #111827;
}
.toggle {
  position: relative;
  width: 48px;
  height: 28px;
  border: 0;
  border-radius: 999px;
  background: #d1d5db;
  cursor: pointer;
  transition: background-color .22s cubic-bezier(.32, .72, 0, 1);
}
.toggle::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, .2);
  transition: transform .22s cubic-bezier(.32, .72, 0, 1);
}
.toggle[aria-checked="true"] { background: #16a34a; }
.toggle[aria-checked="true"]::after { transform: translateX(20px); }
.toggle:focus-visible { outline: 2px solid #4f46e5; outline-offset: 3px; }
@media (prefers-reduced-motion: reduce) {
  .toggle, .toggle::after { transition: none; }
}

演示 HTML 结构

<div class="s d-toggles">
  <div class="d-tg"><span>邮件通知</span><span class="d-tg__sw is-on"></span></div>
  <div class="d-tg"><span>营销推送</span><span class="d-tg__sw"></span></div>
  <div class="d-tg"><span>自动保存</span><span class="d-tg__sw is-on is-on--br"></span></div>
</div>

无障碍要点

  • 用 button role="switch" + aria-checked,或原生 checkbox 视觉隐藏后自定义;状态不能只靠颜色。
  • 整行可点时保证标签与开关用 aria-labelledby 关联。

常见问题

开关的英文是什么?

开关的英文是 Toggle Switch。常见英文别名还包括 iOS Switch。

Toggle Switch 的中文是什么?

Toggle Switch 的中文是「开关」。常见中文别名还包括 切换开关、拨动开关。一颗滑块在轨道里左右滑动,开是绿色关是灰色——比复选框更"物理"的布尔选择。

开态一定要用绿色吗?

可用品牌色;绿色更易被理解为「已开启」。关键是开/关对比足够,且有文字或 aria 状态。

轨道尺寸怎么定?

提示词轨道 44–52×26–30px,示例 48×28;滑块比轨道矮 4px、左右留 2px,开态 translateX 到右侧。

常见搭配

同一页面里经常和「开关」一起出现的模式,可以收藏后用「组合收藏为整页提示词」一起交给 AI。

标签