当前位置:首页 > 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:

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

vue实现switch

标签: vueswitch
分享给朋友:

相关文章

vue里实现增删改查

vue里实现增删改查

在Vue中实现增删改查 数据初始化 在Vue组件的data中定义初始数据和表单绑定的变量: data() { return { items: [ { id: 1, name…

vue架构实现

vue架构实现

Vue 架构实现核心方案 Vue.js 的架构实现通常围绕模块化、组件化和状态管理展开。以下为常见实现方案: 基于单文件组件(SFC)的模块化 通过 .vue 文件组织模板、脚本和样式: &l…

vue实现erp

vue实现erp

Vue实现ERP系统的关键步骤 技术选型与架构设计 采用Vue 3 + TypeScript构建前端,搭配Pinia状态管理,Element Plus或Ant Design Vue作为UI框架。后端可…

vue实现active

vue实现active

Vue 实现 active 状态的方法 在 Vue 中实现 active 状态可以通过以下几种方式实现,具体取决于使用场景和需求。 使用 v-bind:class 动态绑定类名 通过 v-bind…

vue实现树

vue实现树

Vue 实现树形结构的方法 使用递归组件实现树形结构 递归组件是Vue中实现树形结构的常见方式。通过组件调用自身,可以轻松构建多层嵌套的树形数据。 <template> <u…

vue实现list

vue实现list

Vue 实现列表渲染 在 Vue 中,可以使用 v-for 指令来渲染列表。v-for 指令基于一个数组或对象来渲染一个列表,可以遍历数组或对象的属性。 基本列表渲染 <template>…