vue实现switch
Vue 实现 Switch 组件
在 Vue 中实现一个 Switch 组件可以通过多种方式完成,以下是几种常见的实现方法:
使用原生 HTML 和 CSS
通过原生 HTML 的 <input type="checkbox"> 结合 CSS 样式实现 Switch 效果。
<template>
<label class="switch">
<input type="checkbox" v-model="isChecked">
<span class="slider round"></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;
}
.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>
使用第三方 UI 库
许多流行的 Vue UI 库提供了现成的 Switch 组件,可以直接使用。
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="value" color="primary"></v-switch>
</template>
<script>
export default {
data() {
return {
value: true
}
}
}
</script>
自定义可复用 Switch 组件
创建一个可复用的 Switch 组件,支持自定义样式和功能。
Switch.vue:
<template>
<label class="custom-switch">
<input
type="checkbox"
class="custom-switch-input"
:checked="modelValue"
@change="$emit('update:modelValue', $event.target.checked)"
/>
<span class="custom-switch-slider"></span>
</label>
</template>
<script>
export default {
props: {
modelValue: {
type: Boolean,
default: false
}
},
emits: ['update:modelValue']
}
</script>
<style>
.custom-switch {
position: relative;
display: inline-block;
width: 50px;
height: 24px;
}
.custom-switch-input {
opacity: 0;
width: 0;
height: 0;
}
.custom-switch-slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
transition: .4s;
border-radius: 24px;
}
.custom-switch-slider:before {
position: absolute;
content: "";
height: 16px;
width: 16px;
left: 4px;
bottom: 4px;
background-color: white;
transition: .4s;
border-radius: 50%;
}
.custom-switch-input:checked + .custom-switch-slider {
background-color: #4CAF50;
}
.custom-switch-input:checked + .custom-switch-slider:before {
transform: translateX(26px);
}
</style>
使用自定义组件:
<template>
<Switch v-model="isActive" />
</template>
<script>
import Switch from './Switch.vue'
export default {
components: { Switch },
data() {
return {
isActive: false
}
}
}
</script>
动画效果增强
为 Switch 添加更丰富的动画效果,提升用户体验。
.custom-switch-slider {
/* 原有样式 */
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
}
.custom-switch-input:focus + .custom-switch-slider {
box-shadow: 0 0 1px #4CAF50;
}
.custom-switch-slider:before {
/* 原有样式 */
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
}
无障碍支持
确保 Switch 组件对屏幕阅读器等辅助技术友好。
<label class="custom-switch">
<input
type="checkbox"
class="custom-switch-input"
:checked="modelValue"
@change="$emit('update:modelValue', $event.target.checked)"
:aria-checked="modelValue"
role="switch"
/>
<span class="custom-switch-slider" aria-hidden="true"></span>
<span class="visually-hidden">Toggle switch</span>
</label>
<style>
.visually-hidden {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap;
border-width: 0;
}
</style>
以上方法提供了从简单到复杂的 Vue Switch 组件实现方案,可以根据项目需求选择合适的实现方式。







