vue开关按钮具体实现
Vue 开关按钮实现方法
使用原生HTML和Vue数据绑定
通过v-model绑定布尔值控制开关状态,结合CSS样式实现视觉切换:
<template>
<label class="switch">
<input type="checkbox" v-model="isActive">
<span class="slider round"></span>
</label>
</template>
<script>
export default {
data() {
return {
isActive: 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;
}
.slider:before {
position: absolute;
content: "";
height: 26px;
width: 26px;
left: 4px;
bottom: 4px;
background-color: white;
transition: .4s;
}
input:checked + .slider {
background-color: #2196F3;
}
input:checked + .slider:before {
transform: translateX(26px);
}
.slider.round {
border-radius: 34px;
}
.slider.round:before {
border-radius: 50%;
}
</style>
使用第三方组件库
Element UI实现:

<template>
<el-switch v-model="value" active-color="#13ce66" inactive-color="#ff4949">
</el-switch>
</template>
<script>
export default {
data() {
return {
value: true
}
}
}
</script>
Vuetify实现:
<template>
<v-switch v-model="switchValue" color="primary"></v-switch>
</template>
<script>
export default {
data() {
return {
switchValue: false
}
}
}
</script>
自定义可复用组件
创建ToggleSwitch.vue组件:

<template>
<label class="toggle-switch">
<input
type="checkbox"
:checked="modelValue"
@change="$emit('update:modelValue', $event.target.checked)"
>
<span class="switch" :class="{ active: modelValue }"></span>
</label>
</template>
<script>
export default {
props: {
modelValue: {
type: Boolean,
default: false
}
},
emits: ['update:modelValue']
}
</script>
<style>
.toggle-switch {
display: inline-block;
position: relative;
}
.toggle-switch input {
display: none;
}
.switch {
width: 50px;
height: 25px;
background: #ddd;
border-radius: 25px;
display: inline-block;
position: relative;
transition: all 0.3s;
}
.switch:after {
content: '';
position: absolute;
width: 21px;
height: 21px;
border-radius: 50%;
background: white;
top: 2px;
left: 2px;
transition: all 0.3s;
}
.switch.active {
background: #4CAF50;
}
.switch.active:after {
left: calc(100% - 23px);
}
</style>
使用方式:
<template>
<ToggleSwitch v-model="isOn" />
</template>
<script>
import ToggleSwitch from './ToggleSwitch.vue'
export default {
components: { ToggleSwitch },
data() {
return {
isOn: false
}
}
}
</script>
动画增强实现
添加更丰富的过渡效果:
<template>
<div
class="advanced-switch"
:class="{ active: isActive }"
@click="isActive = !isActive"
>
<div class="switch-handle"></div>
</div>
</template>
<style>
.advanced-switch {
width: 60px;
height: 30px;
background: #e0e0e0;
border-radius: 15px;
position: relative;
cursor: pointer;
transition: background 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}
.advanced-switch.active {
background: #4caf50;
}
.switch-handle {
width: 26px;
height: 26px;
background: white;
border-radius: 50%;
position: absolute;
top: 2px;
left: 2px;
box-shadow: 0 2px 4px rgba(0,0,0,0.2);
transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}
.advanced-switch.active .switch-handle {
transform: translateX(30px);
}
</style>
这些实现方法覆盖了从基础到高级的开关按钮需求,可根据项目复杂度选择适合的方案。第三方组件库方案适合快速开发,自定义组件方案则提供更高的灵活性和可定制性。






