Skip to content

Commit f34945e

Browse files
committed
switch组件的基本样式
1 parent 4e9f029 commit f34945e

File tree

3 files changed

+51
-13
lines changed

3 files changed

+51
-13
lines changed

src/App.vue

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,7 @@ import DemoSwitch from './components/DemoSwitch.vue'
33
</script>
44

55
<template>
6-
<div>
7-
<a href="https://vitejs.dev" target="_blank">
8-
<img src="/vite.svg" class="logo" alt="Vite logo" />
9-
</a>
10-
<a href="https://vuejs.org/" target="_blank">
11-
<img src="./assets/vue.svg" class="logo vue" alt="Vue logo" />
12-
</a>
13-
</div>
14-
<DemoSwitch msg="Vite + Vue" />
6+
<DemoSwitch />
157
</template>
168

179
<style scoped>

src/lib/Switch.vue

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,49 @@
11
<script setup lang="ts">
2-
2+
import {ref} from "vue";
3+
let status = ref(false)
4+
const onClick = ()=>{
5+
status.value = !status.value
6+
}
37
</script>
48

59
<template>
6-
<div>
7-
switch
8-
</div>
10+
<div class="zw-switch" :class="status?'zw-switch-on':''" @click="onClick">
11+
<span class="node"></span>
12+
</div>
913
</template>
1014

1115
<style lang="scss" scoped>
16+
@import "style/var.scss";
17+
18+
.zw-switch {
19+
width: $switch-width;
20+
height: $switch-height;
21+
border: $switch-border-color;
22+
border-radius: $switch-border-radius;
23+
background-color: $switch-background-color;
24+
position: relative;
25+
26+
.node {
27+
width: 18px;
28+
height: 18px;
29+
border: $switch-border-color;
30+
background-color: #fff;
31+
display: inline-block;
32+
border-radius: 50%;
33+
position: absolute;
34+
top: 2px;
35+
left: 2px;
36+
transition: transform $switch-transition-duration cubic-bezier(0.3, 1.05, 0.4, 1.05);
37+
}
38+
39+
&-on{
40+
background-color: #1989fa;
41+
box-shadow: 0 0 0 2px rgba(45,140,240,.2);
42+
43+
.node{
44+
transform: translateX($switch-width - 18px - 4px);
45+
}
46+
}
1247
48+
}
1349
</style>

src/lib/style/var.scss

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Animation
2+
$animation-duration-base: 0.3s;
3+
4+
// Switch
5+
$switch-width: 44px;
6+
$switch-height: 22px;
7+
$switch-border-radius: 11px;
8+
$switch-border-color: #ccc;
9+
$switch-background-color: #ccc;
10+
$switch-transition-duration: $animation-duration-base;

0 commit comments

Comments
 (0)