Skip to content

Switch 开关

开关切换组件,用于表示开关两种状态之间的切换。

基础用法

html
<div class="sa-switch" data-switch></div>

<script>
  SA.init('body');
</script>
加载 SanoUI 组件中...
            
          

默认选中

通过 data-checked="true" 设置开关默认选中状态。

html
<div class="sa-switch" data-switch data-checked="true"></div>

<script>
  SA.init('body');
</script>
加载 SanoUI 组件中...
            
          

禁用状态

通过 data-disabled="true" 设置开关为禁用状态。

html
<div class="sa-switch" data-switch data-checked="true" data-disabled="true"></div>

<script>
  SA.init('body');
</script>
加载 SanoUI 组件中...
            
          

带标签的开关

通过 data-labels 配置开关两端的标签文本,格式为 JSON 字符串 ["关闭状态文本", "开启状态文本"]

html
<div class="sa-switch" data-switch data-labels='["关闭", "开启"]'></div>

<script>
  SA.init('body');
</script>
加载 SanoUI 组件中...
            
          

不同尺寸

通过 data-size 配置开关的尺寸。

html
<!-- 大尺寸 -->
<div class="sa-switch" data-switch data-size="large"></div>

<!-- 默认尺寸 -->
<div class="sa-switch" data-switch data-size="default"></div>

<!-- 小尺寸 -->
<div class="sa-switch" data-switch data-size="small"></div>

<script>
  SA.init('body');
</script>
加载 SanoUI 组件中...
            
          

API

构造函数参数

参数说明类型默认值
container容器选择器或元素string | HTMLElement-
options配置选项SaSwitchOptions{}

配置选项

参数说明类型可选值默认值
checked是否选中boolean-false
disabled是否禁用boolean-false
labels标签文本数组Array<string>-null
size开关尺寸stringlarge / default / smalldefault
onChange状态变化回调function(checked: boolean)--

方法

方法名说明参数返回值
setChecked设置选中状态checked: booleanvoid
setDisabled设置禁用状态disabled: booleanvoid
toggle切换状态-void
getChecked获取当前状态-boolean
destroy销毁实例-void

自动初始化属性

属性说明类型默认值
class="sa-switch"启用自动初始化--
data-switch启用自动初始化标记boolean-
data-checked是否选中booleanfalse
data-disabled是否禁用booleanfalse
data-labels标签文本数组(JSON 字符串)string-
data-size开关尺寸stringdefault

示例代码

手动创建实例

javascript
const sw = new SaSwitch('#my-switch', {
  checked: false,
  labels: ['关闭', '开启'],
  onChange: (checked) => {
    console.log('开关状态:', checked);
  }
});

// 设置状态
sw.setChecked(true);

// 切换状态
sw.toggle();

// 获取状态
const isChecked = sw.getChecked();

自动初始化

html
<div class="sa-switch" 
     data-switch 
     data-checked="false"
     data-labels='["关闭", "开启"]'>
</div>

<script>
  SA.init('body');
</script>
加载 SanoUI 组件中...
            
          

实际使用场景

场景 1:功能开关

在设置页面中使用开关控制功能启用/禁用。

html
<div style="max-width: 500px;">
  <div style="display: flex; justify-content: space-between; align-items: center; padding: 1rem; border-bottom: 1px solid #eee;">
    <div>
      <div style="font-weight: 500;">邮件通知</div>
      <div style="font-size: 0.875rem; color: #999; margin-top: 0.25rem;">接收系统邮件通知</div>
    </div>
    <div class="sa-switch" id="email-notify" data-switch data-checked="true"></div>
  </div>
  
  <div style="display: flex; justify-content: space-between; align-items: center; padding: 1rem; border-bottom: 1px solid #eee;">
    <div>
      <div style="font-weight: 500;">短信通知</div>
      <div style="font-size: 0.875rem; color: #999; margin-top: 0.25rem;">接收短信通知</div>
    </div>
    <div class="sa-switch" id="sms-notify" data-switch></div>
  </div>
  
  <div style="display: flex; justify-content: space-between; align-items: center; padding: 1rem;">
    <div>
      <div style="font-weight: 500;">推送通知</div>
      <div style="font-size: 0.875rem; color: #999; margin-top: 0.25rem;">接收推送通知</div>
    </div>
    <div class="sa-switch" id="push-notify" data-switch data-checked="true"></div>
  </div>
</div>

<script>
  SA.init('body');
  
  // 监听开关变化
  document.getElementById('email-notify').addEventListener('change', (e) => {
    console.log('邮件通知:', e.target.checked);
  });
</script>
加载 SanoUI 组件中...
            
          

Released under the MIT License.