vue之switch开关组件的封装
- 效果:
Switch.vue
<template>
<div
@click="swtichClick"
:class="['x_switch', { 'x-switch-checked': value }]"
>
<span class="x_switch_core" ref="core">
<span class="x_switch_btn"></span>
</span>
<input type="checkbox" class="x-switch-input" :name="name" ref="input" />
</div>
</template>
<script>
export default {
name: "Switch",
props: {
name: {
type: String,
default: "",
},
value: {
type: Boolean,
default: false,
},
activeColor: {
type: String,
default: "",
},
inActiveColor: {
type: String,
default: "",
},
},
data() {
return {};
},
mounted() {
this.setColor();
this.$refs.input.checked = this.value;
},
methods: {
swtichClick() {
this.$emit("input", !this.value);
this.$nextTick(() => {
this.setColor();
this.$refs.input.checked = this.value;
});
},
setColor() {
if (this.activeColor || this.inActiveColor) {
let color = this.value ? this.activeColor : this.inActiveColor;
this.$refs.core.style.borderColor = color;
this.$refs.core.style.backgroundColor = color;
}
},
},
components: {},
};
</script>
<style lang="scss" scoped>
.x_switch {
display: inline-flex;
align-items: center;
position: relative;
font-style: 14px;
line-height: 20px;
height: 20px;
vertical-align: middle;
.x-switch-input {
position: absolute;
width: 0;
height: 0;
opacity: 0;
margin: 0;
}
.x_switch_core {
margin: 0;
display: inline-block;
position: relative;
width: 40px;
height: 20px;
border: 1px solid #dcdfe6;
outline: none;
border-radius: 10px;
box-sizing: border-box;
background: #dcdfe6;
cursor: pointer;
transition: border-color 0.3s, background-color 0.3s;
vertical-align: middle;
.x_switch_btn {
position: absolute;
top: 1px;
left: 1px;
border-radius: 100%;
transition: all 0.3s;
width: 16px;
height: 16px;
background: #fff;
}
}
}
.x-switch-checked {
.x_switch_core {
border-color: #409eff;
background-color: #409eff;
.x_switch_btn {
transform: translateX(20px);
}
}
}
</style>
使用组件
<template>
<div class="home">
swFlag {{ swFlag }} <x-switch v-model="swFlag" /> swFlag2 {{ swFlag2 }}
<x-switch
v-model="swFlag2"
activeColor="red"
inActiveColor="green"
name="username"
/>
</div>
</template>
<script>
import XSwitch from "./Test/Switch.vue";
export default {
name: "Home",
components: {
XSwitch,
},
data() {
return {
swFlag: false,
swFlag2: false,
};
},
methods: {
},
};
</script>
<style lang="scss" scoped>
.home {
height: calc(100% - 34px);
width: calc(100% - 34px);
background: #fff;
padding: 16px;
border: 1px solid #ccc;
}
</style>
|