当前位置:首页 > VUE

vue实现switch

2026-01-07 18:15:04VUE

Vue 实现 Switch 开关组件

在 Vue 中实现 Switch 开关组件可以通过自定义组件或使用现有 UI 库完成。以下是几种常见实现方式:

自定义 Switch 组件

创建一个基础 Switch 组件,通过 v-model 实现双向绑定:

vue实现switch

<template>
  <div class="switch" @click="toggle">
    <div class="switch-toggle" :class="{ 'on': value }"></div>
  </div>
</template>

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

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

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

.switch-toggle.on {
  left: 28px;
  background: #4CAF50;
}
</style>

使用第三方 UI 库

Element UI 提供了现成的 Switch 组件:

vue实现switch

<template>
  <el-switch v-model="value" active-color="#13ce66" inactive-color="#ff4949">
  </el-switch>
</template>

<script>
import { ElSwitch } from 'element-ui'

export default {
  components: {
    ElSwitch
  },
  data() {
    return {
      value: false
    }
  }
}
</script>

Vuetify 的 Switch 实现

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

<script>
import { VSwitch } from 'vuetify/lib'

export default {
  components: {
    VSwitch
  },
  data() {
    return {
      value: false
    }
  }
}
</script>

带标签的 Switch 组件

实现一个带有开/关标签的 Switch:

<template>
  <div class="labeled-switch" @click="toggle">
    <span class="label">{{ value ? 'ON' : 'OFF' }}</span>
    <div class="switch" :class="{ 'on': value }"></div>
  </div>
</template>

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

<style>
.labeled-switch {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.label {
  margin-right: 8px;
  font-weight: bold;
}

.switch {
  width: 40px;
  height: 20px;
  background: #ddd;
  border-radius: 10px;
  position: relative;
}

.switch:after {
  content: '';
  position: absolute;
  width: 16px;
  height: 16px;
  background: white;
  border-radius: 50%;
  top: 2px;
  left: 2px;
  transition: all 0.3s;
}

.switch.on {
  background: #4CAF50;
}

.switch.on:after {
  left: 22px;
}
</style>

这些实现方式覆盖了从基础自定义组件到主流UI库的多种方案,可根据项目需求选择合适的实现方法。

标签: vueswitch
分享给朋友:

相关文章

vue   实现单选

vue 实现单选

Vue 实现单选功能 在 Vue 中实现单选功能可以通过多种方式,以下是几种常见的方法: 使用原生 <input type="radio"> 绑定 v-model 通过 v-model…

vue实现过滤

vue实现过滤

Vue实现过滤的方法 在Vue中实现数据过滤可以通过多种方式完成,以下是几种常见的方法: 使用计算属性过滤 计算属性是Vue中处理数据过滤的理想选择,它们基于响应式依赖进行缓存,只有在相关依赖发生改…

vue实现ssr

vue实现ssr

Vue 实现 SSR(服务端渲染) Vue 的服务端渲染(SSR)允许在服务器端生成完整的 HTML 内容,再发送到客户端,提升首屏加载速度和 SEO 友好性。以下是实现 Vue SSR 的关键步骤和…

vue实现mui

vue实现mui

使用 Vue 实现 MUI(Material-UI)风格的界面 Vue 本身不直接提供 MUI(Material Design)组件,但可以通过集成第三方库或手动实现来达到类似效果。以下是几种常见方法…

vue实现筛选

vue实现筛选

实现筛选功能的基本思路 在Vue中实现筛选功能通常需要结合数据绑定、计算属性和方法。筛选的核心逻辑是根据用户输入的条件过滤原始数据列表,并动态更新显示结果。 数据准备 定义一个数组存储原始数据,另一…

vue实现menu

vue实现menu

Vue 实现 Menu 的方法 使用 Element UI 的 Menu 组件 Element UI 提供了现成的 Menu 组件,适合快速实现导航菜单。安装 Element UI 后,可以直接使用…