Swicth 滑动开关

用于展现两个互斥对象的选择,预设 9 种颜色 light, stable, positive, calm, assertive, balanced, energized, royal, dark 可选用。

使用指南

在 page.json 中引入组件

  1. {
  2. "navigationBarTitleText": "Swicth",
  3. "usingComponents": {
  4. "wux-cell-group": "../../dist/cell-group/index",
  5. "wux-cell": "../../dist/cell/index",
  6. "wux-switch": "../../dist/switch/index"
  7. }
  8. }

示例

  1. <view class="page">
  2. <view class="page__hd">
  3. <view class="page__title">Swicth</view>
  4. <view class="page__desc">滑动开关</view>
  5. </view>
  6. <view class="page__bd">
  7. <form bindsubmit="formSubmit">
  8. <wux-cell-group title="Form switch">
  9. <wux-cell title="Light">
  10. <wux-switch name="light" slot="footer" color="light" value="{{ value1 }}" bind:change="onChange1" />
  11. </wux-cell>
  12. <wux-cell title="Stable">
  13. <wux-switch name="stable" slot="footer" color="stable" value="{{ value1 }}" bind:change="onChange1" />
  14. </wux-cell>
  15. <wux-cell title="Positive">
  16. <wux-switch name="positive" slot="footer" color="positive" value="{{ value1 }}" bind:change="onChange1" />
  17. </wux-cell>
  18. <wux-cell title="Calm">
  19. <wux-switch name="calm" slot="footer" color="calm" value="{{ value1 }}" bind:change="onChange1" />
  20. </wux-cell>
  21. <wux-cell title="Balanced">
  22. <wux-switch name="balanced" slot="footer" color="balanced" value="{{ value1 }}" bind:change="onChange1" />
  23. </wux-cell>
  24. <wux-cell title="Energized">
  25. <wux-switch name="energized" slot="footer" color="energized" value="{{ value1 }}" bind:change="onChange1" />
  26. </wux-cell>
  27. <wux-cell title="Assertive">
  28. <wux-switch name="assertive" slot="footer" color="assertive" value="{{ value1 }}" bind:change="onChange1" />
  29. </wux-cell>
  30. <wux-cell title="Royal">
  31. <wux-switch name="royal" slot="footer" color="royal" value="{{ value1 }}" bind:change="onChange1" />
  32. </wux-cell>
  33. <wux-cell title="Dark">
  34. <wux-switch name="dark" slot="footer" color="dark" value="{{ value1 }}" bind:change="onChange1" />
  35. </wux-cell>
  36. <wux-cell title="On">
  37. <wux-switch name="a" slot="footer" value="{{ value2 }}" bind:change="onChange2" />
  38. </wux-cell>
  39. <wux-cell title="Off">
  40. <wux-switch name="b" slot="footer" value="{{ value3 }}" bind:change="onChange3" />
  41. </wux-cell>
  42. <wux-cell title="Custom color">
  43. <wux-switch name="c" slot="footer" value="{{ value4 }}" color="red" bind:change="onChange4" />
  44. </wux-cell>
  45. <wux-cell title="Custom color">
  46. <wux-switch name="d" slot="footer" value="{{ value5 }}" color="yellow" bind:change="onChange5" />
  47. </wux-cell>
  48. <wux-cell title="Disabled on">
  49. <wux-switch name="e" slot="footer" value="{{ true }}" disabled />
  50. </wux-cell>
  51. <wux-cell title="Disabled off">
  52. <wux-switch name="f" slot="footer" value="{{ false }}" disabled />
  53. </wux-cell>
  54. </wux-cell-group>
  55. <view class="btn-area">
  56. <button formType="submit">Submit</button>
  57. </view>
  58. </form>
  59. </view>
  60. </view>
  1. Page({
  2. data: {
  3. thumb: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAC4AAAAuCAMAAABgZ9sFAAAAVFBMVEXx8fHMzMzr6+vn5+fv7+/t7e3d3d2+vr7W1tbHx8eysrKdnZ3p6enk5OTR0dG7u7u3t7ejo6PY2Njh4eHf39/T09PExMSvr6+goKCqqqqnp6e4uLgcLY/OAAAAnklEQVRIx+3RSRLDIAxE0QYhAbGZPNu5/z0zrXHiqiz5W72FqhqtVuuXAl3iOV7iPV/iSsAqZa9BS7YOmMXnNNX4TWGxRMn3R6SxRNgy0bzXOW8EBO8SAClsPdB3psqlvG+Lw7ONXg/pTld52BjgSSkA3PV2OOemjIDcZQWgVvONw60q7sIpR38EnHPSMDQ4MjDjLPozhAkGrVbr/z0ANjAF4AcbXmYAAAAASUVORK5CYII=',
  4. value1: true,
  5. value2: false,
  6. value3: true,
  7. value4: true,
  8. },
  9. onChange(field, e) {
  10. this.setData({
  11. [field]: e.detail.value
  12. })
  13. console.log('radio发生change事件,携带value值为:', e.detail.value)
  14. },
  15. onChange1(e) {
  16. this.onChange('value1', e)
  17. },
  18. onChange2(e) {
  19. this.onChange('value2', e)
  20. },
  21. onChange3(e) {
  22. this.onChange('value3', e)
  23. },
  24. onChange4(e) {
  25. this.onChange('value4', e)
  26. },
  27. onChange5(e) {
  28. this.onChange('value5', e)
  29. },
  30. formSubmit(e) {
  31. console.log('form发生了submit事件,携带数据为:', e.detail.value)
  32. },
  33. })

视频演示

Switch

API

Switch props

参数 类型 描述 默认值
prefixCls string 自定义类名前缀 wux-switch
name string 在表单中的字段名 -
value boolean 在表单中的字段值(当前选中项的值) -
disabled boolean 是否不可修改 false
color string 主题色,可选值为 light、stable、positive、calm、assertive、balanced、energized、royal、dark balanced
bind:change function change 事件触发的回调函数 -

Switch externalClasses

名称 描述
wux-class 根节点样式类