结合 CSS 样式实现开关效果。
当前位置:首页 > VUE

vue switch开关实现

2026-02-16 23:45:16VUE

Vue Switch 开关实现方法

使用原生 HTML 和 CSS

通过原生 HTML 的 <input type="checkbox"> 结合 CSS 样式实现开关效果。

<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:focus + .slider {
  box-shadow: 0 0 1px #2196F3;
}

input:checked + .slider:before {
  transform: translateX(26px);
}

.slider.round {
  border-radius: 34px;
}

.slider.round:before {
  border-radius: 50%;
}
</style>

使用 Element UI 组件

如果项目中使用了 Element UI,可以直接使用其提供的 Switch 组件。

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 组件

在 Vuetify 项目中,可以使用 v-switch 组件实现开关功能。

<template>
  <v-switch v-model="switchValue" color="primary"></v-switch>
</template>

<script>
export default {
  data() {
    return {
      switchValue: false
    }
  }
}
</script>

使用 Ant Design Vue 组件

对于 Ant Design Vue 项目,可以使用 a-switch 组件。

vue switch开关实现

<template>
  <a-switch v-model:checked="checked" />
</template>

<script>
export default {
  data() {
    return {
      checked: false
    }
  }
}
</script>

自定义 Switch 组件

如果需要更高级的定制,可以创建一个可复用的 Switch 组件。

<!-- Switch.vue -->
<template>
  <div class="custom-switch" :class="{ 'is-active': value }" @click="toggle">
    <div class="switch-handle"></div>
  </div>
</template>

<script>
export default {
  props: {
    value: Boolean
  },
  methods: {
    toggle() {
      this.$emit('input', !this.value)
    }
  }
}
</script>

<style>
.custom-switch {
  width: 50px;
  height: 24px;
  background: #ddd;
  border-radius: 12px;
  position: relative;
  cursor: pointer;
  transition: background 0.3s;
}

.custom-switch.is-active {
  background: #4cd964;
}

.switch-handle {
  width: 20px;
  height: 20px;
  background: white;
  border-radius: 50%;
  position: absolute;
  top: 2px;
  left: 2px;
  transition: transform 0.3s;
}

.is-active .switch-handle {
  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>

这些方法涵盖了从简单到复杂的各种 Vue Switch 开关实现方式,可以根据项目需求选择合适的方案。

标签: vueswitch
分享给朋友:

相关文章

vue实现全文检索

vue实现全文检索

Vue 实现全文检索的方法 客户端全文检索(适用于小型数据集) 使用 flexsearch 或 lunr.js 等轻量级库实现前端全文检索: import FlexSearch from 'flex…

vue 实现复选

vue 实现复选

Vue 实现复选功能 在 Vue 中实现复选功能可以通过多种方式完成,以下是常见的几种方法: 使用 v-model 绑定数组 通过 v-model 绑定一个数组,可以自动收集用户选择的复选框值。…

vue实现返回

vue实现返回

返回按钮功能实现 在Vue中实现返回功能通常有两种方式:使用浏览器历史记录的history.back()或结合Vue Router进行编程式导航。以下是具体实现方法: 使用浏览器API met…

vue实现复选

vue实现复选

Vue 实现复选框 在 Vue 中实现复选框可以通过 v-model 指令绑定数据,同时结合 input 元素的 type="checkbox" 属性来实现。以下是几种常见的实现方式: 单个复选框…

vue实现颜色

vue实现颜色

Vue 实现颜色的方法 在 Vue 中实现颜色管理可以通过多种方式,以下是一些常见的方法: 使用 CSS 变量动态绑定颜色 在 Vue 组件的样式中定义 CSS 变量,并通过 Vue 的数据绑定动…

vue实现粘贴

vue实现粘贴

Vue 实现粘贴功能的方法 在 Vue 中实现粘贴功能通常涉及监听粘贴事件并处理剪贴板数据。以下是几种常见的实现方式: 监听原生粘贴事件 通过 @paste 指令或原生 addEventListe…