vue switch开关实现
使用 Vue 实现 Switch 开关组件
基础实现
在 Vue 中可以通过 v-model 和自定义样式实现 Switch 开关。以下是一个基础示例:
<template>
<label class="switch">
<input type="checkbox" v-model="isChecked">
<span class="slider"></span>
</label>
</template>
<script>
export default {
data() {
return {
isChecked: false
}
}
}
</script>
<style>
.switch {
position: relative;
display: inline-block;
width: 60px;
height: 34px;
}
.switch input {
opacity: 0;
width: 0;
height: 0;
}
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
transition: .4s;
border-radius: 34px;
}
.slider:before {
position: absolute;
content: "";
height: 26px;
width: 26px;
left: 4px;
bottom: 4px;
background-color: white;
transition: .4s;
border-radius: 50%;
}
input:checked + .slider {
background-color: #2196F3;
}
input:checked + .slider:before {
transform: translateX(26px);
}
</style>
封装为可复用组件
将 Switch 封装为可复用组件,支持自定义颜色和尺寸:
<template>
<label class="switch" :style="switchStyle">
<input
type="checkbox"
v-model="internalValue"
@change="$emit('change', internalValue)"
>
<span class="slider" :style="sliderStyle"></span>
</label>
</template>
<script>
export default {
props: {
value: Boolean,
activeColor: {
type: String,
default: '#2196F3'
},
inactiveColor: {
type: String,
default: '#ccc'
},
size: {
type: String,
default: 'medium',
validator: value => ['small', 'medium', 'large'].includes(value)
}
},
data() {
return {
internalValue: this.value
}
},
watch: {
value(newVal) {
this.internalValue = newVal
}
},
computed: {
switchStyle() {
const sizes = {
small: { width: '40px', height: '20px' },
medium: { width: '60px', height: '34px' },
large: { width: '80px', height: '40px' }
}
return sizes[this.size]
},
sliderStyle() {
return {
backgroundColor: this.internalValue ? this.activeColor : this.inactiveColor
}
}
}
}
</script>
<style scoped>
.switch {
position: relative;
display: inline-block;
}
.switch input {
opacity: 0;
width: 0;
height: 0;
}
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
transition: .4s;
border-radius: 34px;
}
.slider:before {
position: absolute;
content: "";
height: calc(100% - 8px);
width: calc(50% - 8px);
left: 4px;
bottom: 4px;
background-color: white;
transition: .4s;
border-radius: 50%;
}
input:checked + .slider:before {
transform: translateX(100%);
}
</style>
使用 Element UI 的 Switch 组件
如果使用 Element UI 框架,可以直接使用其提供的 Switch 组件:
<template>
<el-switch
v-model="value"
active-color="#13ce66"
inactive-color="#ff4949">
</el-switch>
</template>
<script>
export default {
data() {
return {
value: true
}
}
}
</script>
实现带有动画效果的 Switch
添加更丰富的动画效果可以增强用户体验:
<template>
<label class="switch" @click.prevent="toggle">
<input type="checkbox" v-model="isActive" hidden>
<span class="slider" :class="{ 'active': isActive }">
<span class="knob" :class="{ 'active': isActive }"></span>
</span>
<span class="label">{{ isActive ? 'ON' : 'OFF' }}</span>
</label>
</template>
<script>
export default {
data() {
return {
isActive: false
}
},
methods: {
toggle() {
this.isActive = !this.isActive
this.$emit('change', this.isActive)
}
}
}
</script>
<style scoped>
.switch {
display: flex;
align-items: center;
cursor: pointer;
}
.slider {
width: 60px;
height: 30px;
background-color: #e0e0e0;
border-radius: 15px;
position: relative;
transition: all 0.3s ease;
}
.slider.active {
background-color: #4CAF50;
}
.knob {
width: 26px;
height: 26px;
background-color: white;
border-radius: 50%;
position: absolute;
top: 2px;
left: 2px;
transition: all 0.3s ease;
box-shadow: 0 2px 5px rgba(0,0,0,0.2);
}
.knob.active {
left: calc(100% - 28px);
}
.label {
margin-left: 10px;
font-weight: bold;
color: #333;
}
</style>






