1 | <template>
|
2 | <label class="mint-switch">
|
3 | <input class="mint-switch-input" :disabled="disabled" @change="$emit('change', currentValue)" type="checkbox" v-model="currentValue">
|
4 | <span class="mint-switch-core"></span>
|
5 | <div class="mint-switch-label"><slot></slot></div>
|
6 | </label>
|
7 | </template>
|
8 |
|
9 | <script>
|
10 | /**
|
11 | * mt-switch
|
12 | * @module components/switch
|
13 | * @desc 切换按钮
|
14 | * @param {boolean} [value] - 绑定值,支持双向绑定
|
15 | * @param {slot} - 显示内容
|
16 | *
|
17 | * @example
|
18 | * <mt-switch v-model="value"></mt-switch>
|
19 | */
|
20 | export default {
|
21 | name: 'mt-switch',
|
22 |
|
23 | props: {
|
24 | value: Boolean,
|
25 | disabled: {
|
26 | type: Boolean,
|
27 | default: false
|
28 | }
|
29 | },
|
30 | computed: {
|
31 | currentValue: {
|
32 | get() {
|
33 | return this.value;
|
34 | },
|
35 | set(val) {
|
36 | this.$emit('input', val);
|
37 | }
|
38 | }
|
39 | }
|
40 | };
|
41 | </script>
|
42 |
|
43 | <style lang="css">
|
44 | @import "../../../src/style/var.css";
|
45 |
|
46 | @component-namespace mint {
|
47 | @component switch {
|
48 | display: flex;
|
49 | align-items: center;
|
50 | position: relative;
|
51 |
|
52 | * {
|
53 | pointer-events: none;
|
54 | }
|
55 |
|
56 | @descendent label {
|
57 | margin-left: 10px;
|
58 | display: inline-block;
|
59 |
|
60 | &:empty {
|
61 | margin-left: 0;
|
62 | }
|
63 | }
|
64 |
|
65 | @descendent core {
|
66 | display: inline-block;
|
67 | position: relative;
|
68 | size: 52px 32px;
|
69 | border: 1px solid $color-grey;
|
70 | border-radius: 16px;
|
71 | box-sizing: border-box;
|
72 | background: $color-grey;
|
73 |
|
74 | &::after, &::before {
|
75 | content: " ";
|
76 | position: absolute 0 * * 0;
|
77 | transition:transform .3s;
|
78 | border-radius: 15px;
|
79 | }
|
80 |
|
81 | &::after {
|
82 | size: 30px;
|
83 | background-color: $color-white;
|
84 | box-shadow: 0 1px 3px rgba(0, 0, 0, .4);
|
85 | }
|
86 |
|
87 | &::before {
|
88 | size: 50px 30px;
|
89 | background-color: #fdfdfd;
|
90 | }
|
91 | }
|
92 |
|
93 | @descendent input {
|
94 | display: none;
|
95 |
|
96 | &:checked {
|
97 | + .mint-switch-core {
|
98 | border-color: $color-blue;
|
99 | background-color: $color-blue;
|
100 |
|
101 | &::before {
|
102 | transform: scale(0);
|
103 | }
|
104 |
|
105 | &::after {
|
106 | transform: translateX(20px);
|
107 | }
|
108 | }
|
109 | }
|
110 | }
|
111 | }
|
112 | }
|
113 | </style>
|