当前位置:首页 > VUE

vue实现switch

2026-02-10 02:08:40VUE

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:

vue实现switch

<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:

vue实现switch

<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 组件实现方案,可以根据项目需求选择合适的实现方式。

标签: vueswitch
分享给朋友:

相关文章

vue的实现

vue的实现

Vue 的实现原理 Vue 是一个渐进式 JavaScript 框架,其核心实现主要包括响应式系统、虚拟 DOM、模板编译和组件化机制。以下是 Vue 实现的主要技术细节。 响应式系统 Vue 的响…

vue实现pie

vue实现pie

Vue 实现饼图 在 Vue 中实现饼图通常可以通过第三方图表库(如 ECharts、Chart.js 或 Highcharts)来完成。以下是使用 ECharts 和 Chart.js 的两种常见方…

vue键盘实现

vue键盘实现

监听键盘事件 在Vue中监听键盘事件可以通过v-on指令或@缩写实现。常见的键盘事件包括keydown、keyup和keypress。例如监听回车键事件: <template> &l…

vue远离实现

vue远离实现

Vue 原理实现 Vue.js 的核心原理包括响应式系统、虚拟 DOM、模板编译和组件化机制。以下是其关键实现原理的详细解析。 响应式系统 Vue 的响应式系统基于 Object.definePro…

vue递归实现

vue递归实现

vue递归实现方法 在Vue中实现递归组件通常用于树形结构、嵌套菜单等场景。以下是具体实现方式: 使用组件name属性递归 定义一个组件时,通过name属性让组件可以在自身模板中调用自己:…

vue实现tab

vue实现tab

Vue 实现 Tab 切换功能 在 Vue 中实现 Tab 切换功能可以通过多种方式完成,以下是几种常见的实现方法: 使用 v-if 和 v-show 指令 通过绑定 v-if 或 v-show 来…