表单
开关
珍珠白滑块滑到墨色轨道上,适用于立即生效的设置。
本页内容
开关以凹槽为轨道,珍珠白滑块沿轨道滑到墨色区域。对于即时生效的设置,在原生复选框上使用 .switch,并设置 role="switch"。对于随表单提交的选项,使用复选框更清楚。
开关
<label class="check"> <input type="checkbox" role="switch" class="switch" checked> Email notifications</label><label className="check"> <input type="checkbox" role="switch" className="switch" defaultChecked /> Email notifications</label><label class="check"> <input type="checkbox" role="switch" class="switch" checked> Email notifications</label><template> <label class="check"> <input type="checkbox" role="switch" class="switch" checked> Email notifications </label></template>#尺寸
尺寸
<input type="checkbox" role="switch" class="switch switch-sm" aria-label="Small" checked><input type="checkbox" role="switch" class="switch" aria-label="Medium" checked><input type="checkbox" role="switch" class="switch switch-lg" aria-label="Large" checked><> <input type="checkbox" role="switch" className="switch switch-sm" aria-label="Small" defaultChecked /> <input type="checkbox" role="switch" className="switch" aria-label="Medium" defaultChecked /> <input type="checkbox" role="switch" className="switch switch-lg" aria-label="Large" defaultChecked /></><input type="checkbox" role="switch" class="switch switch-sm" aria-label="Small" checked><input type="checkbox" role="switch" class="switch" aria-label="Medium" checked><input type="checkbox" role="switch" class="switch switch-lg" aria-label="Large" checked><template> <input type="checkbox" role="switch" class="switch switch-sm" aria-label="Small" checked> <input type="checkbox" role="switch" class="switch" aria-label="Medium" checked> <input type="checkbox" role="switch" class="switch switch-lg" aria-label="Large" checked></template>#在设置列表中使用
设置列表
<div class="sheet flex flex-col p(0)"> <label class="check justify-between gap-lg px-lg py-md"> <span>Automatic updates</span> <input type="checkbox" role="switch" class="switch" checked> </label> <label class="check justify-between gap-lg px-lg py-md bt(1) bt-solid bt-line-hair"> <span>Usage analytics</span> <input type="checkbox" role="switch" class="switch"> </label></div><div className="sheet flex flex-col p(0)"> <label className="check justify-between gap-lg px-lg py-md"> <span>Automatic updates</span> <input type="checkbox" role="switch" className="switch" defaultChecked /> </label> <label className="check justify-between gap-lg px-lg py-md bt(1) bt-solid bt-line-hair"> <span>Usage analytics</span> <input type="checkbox" role="switch" className="switch" /> </label></div><div class="sheet flex flex-col p(0)"> <label class="check justify-between gap-lg px-lg py-md"> <span>Automatic updates</span> <input type="checkbox" role="switch" class="switch" checked> </label> <label class="check justify-between gap-lg px-lg py-md bt(1) bt-solid bt-line-hair"> <span>Usage analytics</span> <input type="checkbox" role="switch" class="switch"> </label></div><template> <div class="sheet flex flex-col p(0)"> <label class="check justify-between gap-lg px-lg py-md"> <span>Automatic updates</span> <input type="checkbox" role="switch" class="switch" checked> </label> <label class="check justify-between gap-lg px-lg py-md bt(1) bt-solid bt-line-hair"> <span>Usage analytics</span> <input type="checkbox" role="switch" class="switch"> </label> </div></template>#禁用
禁用
<input type="checkbox" role="switch" class="switch" aria-label="Beta features" disabled><input type="checkbox" role="switch" class="switch" aria-label="Security alerts" checked disabled><> <input type="checkbox" role="switch" className="switch" aria-label="Beta features" disabled /> <input type="checkbox" role="switch" className="switch" aria-label="Security alerts" defaultChecked disabled /></><input type="checkbox" role="switch" class="switch" aria-label="Beta features" disabled><input type="checkbox" role="switch" class="switch" aria-label="Security alerts" checked disabled><template> <input type="checkbox" role="switch" class="switch" aria-label="Beta features" disabled> <input type="checkbox" role="switch" class="switch" aria-label="Security alerts" checked disabled></template>#无障碍
- 保留
role="switch",让辅助技术将选中状态读作开启,将未选中状态读作关闭。 - 每个开关都需要标签,可以使用包裹控件的
<label>,也可以使用<label for>或aria-label。 - 输入控件保留原生行为,可以聚焦,聚焦后按空格键即可切换状态。
#类名
| 类名 | 说明 |
|---|---|
switch | 带 role="switch" 的原生复选框。轨道高 22px,滑块与轨道边缘相距 3px。在触摸屏上,触控区域扩展至 44px,轨道尺寸保持不变。文字从右向左排列时,滑块的移动方向反转。 |
switch-sm | 轨道高 18px,默认高度为 22px。与 switch 一同使用 |
switch-lg | 轨道高 26px,默认高度为 22px。与 switch 一同使用 |