blob: dd8e4bb114aa36be2048118785ae25eb2ed5b61e [file] [log] [blame]
guangchao.xu070005a2020-12-07 09:56:40 +08001<template>
2 <view class="u-switch" :class="[value == true ? 'u-switch--on' : '', disabled ? 'u-switch--disabled' : '']" @tap="onClick"
3 :style="[switchStyle]">
4 <view class="u-switch__node node-class" :style="{
5 width: $u.addUnit(this.size),
6 height: $u.addUnit(this.size)
7 }">
8 <u-loading :show="loading" class="u-switch__loading" :size="size * 0.6" :color="loadingColor" />
9 </view>
10 </view>
11</template>
12
13<script>
14 /**
15 * switch 开关选择器
16 * @description 选择开关一般用于只有两个选择,且只能选其一的场景。
17 * @tutorial https://www.uviewui.com/components/switch.html
18 * @property {Boolean} loading 是否处于加载中(默认false)
19 * @property {Boolean} disabled 是否禁用(默认false)
20 * @property {String Number} size 开关尺寸,单位rpx(默认50)
21 * @property {String} active-color 打开时的背景色(默认#2979ff)
22 * @property {Boolean} inactive-color 关闭时的背景色(默认#ffffff)
23 * @property {Boolean | Number | String} active-value 打开选择器时通过change事件发出的值(默认true)
24 * @property {Boolean | Number | String} inactive-value 关闭选择器时通过change事件发出的值(默认false)
25 * @event {Function} change 在switch打开或关闭时触发
26 * @example <u-switch v-model="checked" active-color="red" inactive-color="#eee"></u-switch>
27 */
28 export default {
29 name: "u-switch",
30 props: {
31 // 是否为加载中状态
32 loading: {
33 type: Boolean,
34 default: false
35 },
36 // 是否为禁用装填
37 disabled: {
38 type: Boolean,
39 default: false
40 },
41 // 开关尺寸,单位rpx
42 size: {
43 type: [Number, String],
44 default: 50
45 },
46 // 打开时的背景颜色
47 activeColor: {
48 type: String,
49 default: '#2979ff'
50 },
51 // 关闭时的背景颜色
52 inactiveColor: {
53 type: String,
54 default: '#ffffff'
55 },
56 // 通过v-model双向绑定的值
57 value: {
58 type: Boolean,
59 default: false
60 },
61 // 是否使手机发生短促震动,目前只在iOS的微信小程序有效(2020-05-06)
62 vibrateShort: {
63 type: Boolean,
64 default: false
65 },
66 // 打开选择器时的值
67 activeValue: {
68 type: [Number, String, Boolean],
69 default: true
70 },
71 // 关闭选择器时的值
72 inactiveValue: {
73 type: [Number, String, Boolean],
74 default: false
75 },
76 },
77 data() {
78 return {
79
80 }
81 },
82 computed: {
83 switchStyle() {
84 let style = {};
85 style.fontSize = this.size + 'rpx';
86 style.backgroundColor = this.value ? this.activeColor : this.inactiveColor;
87 return style;
88 },
89 loadingColor() {
90 return this.value ? this.activeColor : null;
91 }
92 },
93 methods: {
94 onClick() {
95 if (!this.disabled && !this.loading) {
96 // 使手机产生短促震动,微信小程序有效,APP(HX 2.6.8)和H5无效
97 if(this.vibrateShort) uni.vibrateShort();
98 this.$emit('input', !this.value);
99 // 放到下一个生命周期,因为双向绑定的value修改父组件状态需要时间,且是异步的
100 this.$nextTick(() => {
101 this.$emit('change', this.value ? this.activeValue : this.inactiveValue);
102 })
103 }
104 }
105 }
106 };
107</script>
108
109<style lang="scss" scoped>
110 @import "../../libs/css/style.components.scss";
111
112 .u-switch {
113 position: relative;
114 /* #ifndef APP-NVUE */
115 display: inline-block;
116 /* #endif */
117 box-sizing: initial;
118 width: 2em;
119 height: 1em;
120 background-color: #fff;
121 border: 1px solid rgba(0, 0, 0, 0.1);
122 border-radius: 1em;
123 transition: background-color 0.3s;
124 font-size: 50rpx;
125 }
126
127 .u-switch__node {
128 @include vue-flex;
129 align-items: center;
130 justify-content: center;
131 position: absolute;
132 top: 0;
133 left: 0;
134 border-radius: 100%;
135 z-index: 1;
136 background-color: #fff;
137 background-color: #fff;
138 box-shadow: 0 3px 1px 0 rgba(0, 0, 0, 0.05), 0 2px 2px 0 rgba(0, 0, 0, 0.1), 0 3px 3px 0 rgba(0, 0, 0, 0.05);
139 box-shadow: 0 3px 1px 0 rgba(0, 0, 0, 0.05), 0 2px 2px 0 rgba(0, 0, 0, 0.1), 0 3px 3px 0 rgba(0, 0, 0, 0.05);
140 transition: transform 0.3s cubic-bezier(0.3, 1.05, 0.4, 1.05);
141 transition: transform 0.3s cubic-bezier(0.3, 1.05, 0.4, 1.05), -webkit-transform 0.3s cubic-bezier(0.3, 1.05, 0.4, 1.05);
142 transition: transform cubic-bezier(0.3, 1.05, 0.4, 1.05);
143 transition: transform 0.3s cubic-bezier(0.3, 1.05, 0.4, 1.05)
144 }
145
146 .u-switch__loading {
147 @include vue-flex;
148 align-items: center;
149 justify-content: center;
150 }
151
152 .u-switch--on {
153 background-color: #1989fa;
154 }
155
156 .u-switch--on .u-switch__node {
157 transform: translateX(100%);
158 }
159
160 .u-switch--disabled {
161 opacity: 0.4;
162 }
163</style>