Skip to content

Commit b908c3b

Browse files
committed
switch样式优化
1 parent f632e2a commit b908c3b

File tree

1 file changed

+47
-36
lines changed

1 file changed

+47
-36
lines changed

src/lib/Switch.vue

Lines changed: 47 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,66 @@
11
<template>
2-
<button @click="toggle" :class="{checked:value}"> <span></span> </button>
2+
<button @click="toggle" :class="{checked:value}"><span></span></button>
33
</template>
44
<script lang="ts">
55
export default {
6-
props:{
7-
value:Boolean
6+
props: {
7+
value: Boolean
88
},
99
// props和context 传值方式
10-
setup(props,context){
11-
const toggle = ()=>{
10+
setup(props, context) {
11+
const toggle = () => {
1212
// 事件:vue3的v-model改动,之前是input
13-
context.emit('update:value', !props.value)
14-
}
15-
return { toggle }
13+
context.emit('update:value', !props.value);
14+
};
15+
return {toggle};
1616
}
17-
}
17+
};
1818
</script>
1919
<style lang="scss" scoped>
2020
$h: 22px;
2121
$h2: $h - 4px;
22-
button{
22+
button {
2323
height: $h;
24-
width: $h*2;
24+
width: $h * 2;
2525
border: none;
2626
background: #bfbfbf;
2727
border-radius: $h/2;
2828
position: relative;
29-
}
30-
span{
31-
position: absolute;
32-
top: 2px;
33-
left: 2px;
34-
height: $h2;
35-
width: $h2;
36-
background:white;
37-
border-radius: $h2 / 2;
38-
transition: all 250ms;
39-
}
40-
button.checked{
41-
background: #1890ff;
42-
}
43-
button.checked > span {
44-
left: calc(100% - #{$h2} - 2px);
45-
}
46-
button:focus{
47-
outline: none;
48-
}
49-
button:active{
50-
> span {width: $h2 + 4px;}
51-
}
52-
button.checked:active{
53-
> span {width: $h2 + 4px; margin-left: -4px;}
29+
30+
> span {
31+
position: absolute;
32+
top: 2px;
33+
left: 2px;
34+
height: $h2;
35+
width: $h2;
36+
background: white;
37+
border-radius: $h2 / 2;
38+
transition: all 250ms;
39+
}
40+
41+
&.checked {
42+
background: #1890ff;
43+
44+
> span {
45+
left: calc(100% - #{$h2} - 2px);
46+
}
47+
}
48+
49+
&:focus {
50+
outline: none;
51+
}
52+
53+
&:active {
54+
> span {
55+
width: $h2 + 4px;
56+
}
57+
}
58+
59+
&.checked:active {
60+
> span {
61+
width: $h2 + 4px;
62+
margin-left: -4px;
63+
}
64+
}
5465
}
5566
</style>

0 commit comments

Comments
 (0)