Blame view

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