-
Notifications
You must be signed in to change notification settings - Fork 10
/
Switch.vue
141 lines (141 loc) · 3.41 KB
/
Switch.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<template>
<div class="wt-switch">
<label class="label-switch">
<input type="checkbox" :disabled="disable" @click="Switch" :checked="checked">
<div :class="{checkbox: true,disabled:disable}" ref="switchRef"></div>
</label>
</div>
</template>
<script>
import {ref, watchEffect, onMounted} from 'vue';
export default {
props: {
disable: {
type: Boolean,
default: ()=> {
return false;
}
},
checked: {
type: Boolean,
default: ()=> {
return false;
}
},
color: {
type: String,
default: ()=> {
return '#4cd964';
}
}
},
setup(props, {emit}) {
const switchRef = ref(null)
const Switch =()=> {
emit('switch', event.target.checked);
if(event.target.checked){
switchRef.value.style.background = props.color;
} else {
switchRef.value.style.background = "#e5e5e5";
}
};
onMounted(() => {
const stop = watchEffect(() => {
if(props.checked){
switchRef.value.style.background = props.color;
}
})
// 清除监听
stop()
})
// 暴露给模板
return {
switchRef,
Switch
}
}
};
</script>
<style scoped lang="less" rel="stylesheet/less">
.wt-switch .label-switch {
display: block;
vertical-align: middle;
width: 2.6rem;
border-radius: 0.8rem;
box-sizing: border-box;
height: 1.7rem;
position: relative;
cursor: pointer;
-ms-flex-item-align: center;
-webkit-align-self: center;
align-self: center;
input[type="checkbox"] {
position: absolute;
z-index: 2;
opacity: 0;
width: 2.6rem;
height: 1.7rem;
}
input[type="checkbox"]:checked + .checkbox {
background: #4cd964;
}
input[type="checkbox"]:checked + .checkbox:after {
transform: translateX(1.1rem);
}
.checkbox {
width: 2.6rem;
border-radius: 0.8rem;
box-sizing: border-box;
height: 1.7rem;
background: #e5e5e5;
z-index: 0;
margin: 0;
padding: 0;
-webkit-appearance: none;
-moz-appearance: none;
-ms-appearance: none;
appearance: none;
border: none;
cursor: pointer;
position: relative;
-webkit-transition-duration: 300ms;
transition-duration: 300ms;
&.disabled {
&:after {
background: #ebe8e8;
}
}
&:before {
content: ' ';
position: absolute;
left: 0.1rem;
top: 0.1rem;
width: 2.4rem;
border-radius: 0.8rem;
box-sizing: border-box;
height: 1.4rem;
z-index: 1;
-webkit-transition-duration: 300ms;
transition-duration: 300ms;
-webkit-transform: scale(1);
transform: scale(1);
}
&:after {
content: ' ';
height: 1.4rem;
width: 1.4rem;
border-radius: 1.4rem;
background: #fff;
position: absolute;
z-index: 2;
top: 0.1rem;
left: 0.1rem;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.4);
-webkit-transform: translateX(0px);
transform: translateX(0px);
-webkit-transition-duration: 300ms;
transition-duration: 300ms;
}
}
}
</style>