基本开关
开关的工作原理是将一个不可见的复选框层叠在自定义开关指示器之上。这在保留交互性的同时,为你提供了一个完全可自定义的底层。开关还包含 switch 属性,以便在支持它的浏览器中切换时提供触觉反馈。
没有可见标签的开关仍然需要一个可供辅助技术使用的无障碍名称,例如使用 aria-label。该名称应说明开关打开或关闭的内容,例如 aria-label="Email notifications",并替换下面示例中的 ... 占位符。详情请参阅表单概览无障碍部分。
<div class="switch">
<input type="checkbox" value="" id="switch" role="switch" switch aria-label="...">
</div>配合表单字段
将 .switch 包裹在 .form-field 布局包装器中,并添加你的标签。网格布局会将开关和标签并排显示。
<div class="form-field">
<div class="switch">
<input type="checkbox" value="" id="switchMdLabel" role="switch" switch>
</div>
<label for="switchMdLabel">Default switch</label>
</div>主题颜色
通过向 .switch 元素添加 .theme-{color} 类来修改开关的外观。这会将选中的背景和边框颜色设置为对应的主题颜色。
<div class="form-field">
<div class="switch theme-primary">
<input type="checkbox" value="" id="switchprimary" role="switch" switch checked>
</div>
<label for="switchprimary">Example primary switch</label>
</div>
<div class="form-field">
<div class="switch theme-accent">
<input type="checkbox" value="" id="switchaccent" role="switch" switch checked>
</div>
<label for="switchaccent">Example accent switch</label>
</div>
<div class="form-field">
<div class="switch theme-success">
<input type="checkbox" value="" id="switchsuccess" role="switch" switch checked>
</div>
<label for="switchsuccess">Example success switch</label>
</div>
<div class="form-field">
<div class="switch theme-danger">
<input type="checkbox" value="" id="switchdanger" role="switch" switch checked>
</div>
<label for="switchdanger">Example danger switch</label>
</div>
<div class="form-field">
<div class="switch theme-warning">
<input type="checkbox" value="" id="switchwarning" role="switch" switch checked>
</div>
<label for="switchwarning">Example warning switch</label>
</div>
<div class="form-field">
<div class="switch theme-info">
<input type="checkbox" value="" id="switchinfo" role="switch" switch checked>
</div>
<label for="switchinfo">Example info switch</label>
</div>
<div class="form-field">
<div class="switch theme-inverse">
<input type="checkbox" value="" id="switchinverse" role="switch" switch checked>
</div>
<label for="switchinverse">Example inverse switch</label>
</div>
<div class="form-field">
<div class="switch theme-secondary">
<input type="checkbox" value="" id="switchsecondary" role="switch" switch checked>
</div>
<label for="switchsecondary">Example secondary switch</label>
</div>禁用
添加 disabled 属性,关联的 <label> 会自动应用较浅的颜色样式以匹配,帮助指示输入框的状态。
<div class="form-field">
<div class="switch">
<input type="checkbox" value="" id="switchDisabledLabel" role="switch" switch disabled>
</div>
<label for="switchDisabledLabel">Disabled switch</label>
</div>
<div class="form-field">
<div class="switch">
<input type="checkbox" value="" id="switchCheckedDisabledLabel" role="switch" switch disabled checked>
</div>
<label for="switchCheckedDisabledLabel">Disabled checked switch</label>
</div>尺寸
添加尺寸修饰符类使开关显示得更小或更大。
<div class="form-field">
<div class="switch switch-sm">
<input type="checkbox" value="" id="switchSizeSm" role="switch" switch>
</div>
<label for="switchSizeSm">Small switch</label>
</div>
<div class="form-field">
<div class="switch">
<input type="checkbox" value="" id="switchSizeMd" role="switch" switch>
</div>
<label for="switchSizeMd">Default switch</label>
</div>
<div class="form-field">
<div class="switch switch-lg">
<input type="checkbox" value="" id="switchSizeLg" role="switch" switch>
</div>
<label for="switchSizeLg">Large switch</label>
</div>自定义宽度
通过覆盖 CSS 变量,为任何宽度和指示器尺寸自定义开关。例如,要获得更现代的外观,你可以加宽开关和指示器。
<div class="form-field">
<div class="switch" style="--bs-switch-width: 2.75rem; --bs-switch-indicator-width: 1.5rem;">
<input type="checkbox" value="" id="switchCustomWidth" role="switch" switch>
</div>
<label for="switchCustomWidth">Custom width switch</label>
</div>CSS
变量
// stylelint-disable-next-line scss/dollar-variable-default
$switch-tokens: defaults(
(
--switch-height: 1.25rem,
--switch-width: calc(var(--switch-height) * 1.75),
--switch-padding: .0625rem,
--switch-margin-block: .125rem,
// Share the form-control motion tokens so switch, checkbox, and radio move
// in sync; the overshoot ("back") easing makes the thumb bounce as it settles.
--switch-transition-duration: var(--control-transition-duration),
--switch-transition-timing: var(--control-transition-timing),
--switch-bg: var(--bg-3),
--switch-border-width: var(--border-width),
--switch-border-color: var(--border-color),
--switch-indicator-bg: var(--white),
--switch-indicator-width: calc(var(--switch-height) - calc(var(--switch-padding) * 2) - var(--switch-border-width) * 2),
--switch-indicator-height: calc(var(--switch-height) - calc(var(--switch-padding) * 2) - var(--switch-border-width) * 2),
--switch-checked-bg: var(--control-checked-bg),
--switch-checked-border-color: var(--switch-checked-bg),
--switch-checked-indicator-bg: var(--white),
--switch-disabled-bg: var(--control-disabled-bg),
--switch-disabled-indicator-bg: var(--fg-3),
),
$switch-tokens
);