当前位置:首页 > VUE

vue switch开关实现

2026-01-15 22:16:05VUE

使用 Vue 实现 Switch 开关组件

基础实现

在 Vue 中可以通过 v-model 和自定义样式实现 Switch 开关。以下是一个基础示例:

<template>
  <label class="switch">
    <input type="checkbox" v-model="isChecked">
    <span class="slider"></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;
  border-radius: 34px;
}

.slider:before {
  position: absolute;
  content: "";
  height: 26px;
  width: 26px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  transition: .4s;
  border-radius: 50%;
}

input:checked + .slider {
  background-color: #2196F3;
}

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

封装为可复用组件

将 Switch 封装为可复用组件,支持自定义颜色和尺寸:

<template>
  <label class="switch" :style="switchStyle">
    <input 
      type="checkbox" 
      v-model="internalValue"
      @change="$emit('change', internalValue)"
    >
    <span class="slider" :style="sliderStyle"></span>
  </label>
</template>

<script>
export default {
  props: {
    value: Boolean,
    activeColor: {
      type: String,
      default: '#2196F3'
    },
    inactiveColor: {
      type: String,
      default: '#ccc'
    },
    size: {
      type: String,
      default: 'medium',
      validator: value => ['small', 'medium', 'large'].includes(value)
    }
  },
  data() {
    return {
      internalValue: this.value
    }
  },
  watch: {
    value(newVal) {
      this.internalValue = newVal
    }
  },
  computed: {
    switchStyle() {
      const sizes = {
        small: { width: '40px', height: '20px' },
        medium: { width: '60px', height: '34px' },
        large: { width: '80px', height: '40px' }
      }
      return sizes[this.size]
    },
    sliderStyle() {
      return {
        backgroundColor: this.internalValue ? this.activeColor : this.inactiveColor
      }
    }
  }
}
</script>

<style scoped>
.switch {
  position: relative;
  display: inline-block;
}

.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  transition: .4s;
  border-radius: 34px;
}

.slider:before {
  position: absolute;
  content: "";
  height: calc(100% - 8px);
  width: calc(50% - 8px);
  left: 4px;
  bottom: 4px;
  background-color: white;
  transition: .4s;
  border-radius: 50%;
}

input:checked + .slider:before {
  transform: translateX(100%);
}
</style>

使用 Element UI 的 Switch 组件

如果使用 Element UI 框架,可以直接使用其提供的 Switch 组件:

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

<script>
export default {
  data() {
    return {
      value: true
    }
  }
}
</script>

实现带有动画效果的 Switch

添加更丰富的动画效果可以增强用户体验:

vue switch开关实现

<template>
  <label class="switch" @click.prevent="toggle">
    <input type="checkbox" v-model="isActive" hidden>
    <span class="slider" :class="{ 'active': isActive }">
      <span class="knob" :class="{ 'active': isActive }"></span>
    </span>
    <span class="label">{{ isActive ? 'ON' : 'OFF' }}</span>
  </label>
</template>

<script>
export default {
  data() {
    return {
      isActive: false
    }
  },
  methods: {
    toggle() {
      this.isActive = !this.isActive
      this.$emit('change', this.isActive)
    }
  }
}
</script>

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

.slider {
  width: 60px;
  height: 30px;
  background-color: #e0e0e0;
  border-radius: 15px;
  position: relative;
  transition: all 0.3s ease;
}

.slider.active {
  background-color: #4CAF50;
}

.knob {
  width: 26px;
  height: 26px;
  background-color: white;
  border-radius: 50%;
  position: absolute;
  top: 2px;
  left: 2px;
  transition: all 0.3s ease;
  box-shadow: 0 2px 5px rgba(0,0,0,0.2);
}

.knob.active {
  left: calc(100% - 28px);
}

.label {
  margin-left: 10px;
  font-weight: bold;
  color: #333;
}
</style>

标签: vueswitch
分享给朋友:

相关文章

vue实现展开与收起

vue实现展开与收起

Vue 实现展开与收起功能 在 Vue 中实现展开与收起功能可以通过多种方式完成,以下是一些常见的方法: 使用 v-show 或 v-if 控制显示 通过 Vue 的指令 v-show 或 v-if…

vue实现点击

vue实现点击

实现点击事件的基本方法 在Vue中实现点击事件可以通过v-on指令或@简写方式绑定。以下是常见的点击事件绑定方法: <template> <button v-on:click=…

vue实现主页

vue实现主页

Vue 实现主页的基本步骤 创建一个 Vue 主页通常涉及以下几个关键步骤,从项目初始化到页面布局和功能实现。 初始化 Vue 项目 使用 Vue CLI 或 Vite 初始化项目。Vue CLI…

vue实现granfana

vue实现granfana

Vue 实现类似 Grafana 的仪表盘 要在 Vue 中实现类似 Grafana 的仪表盘功能,需要结合数据可视化库、状态管理和 UI 组件。以下是关键步骤和推荐工具: 数据可视化库 使用 E…

vue实现switch

vue实现switch

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

vue实现动态

vue实现动态

Vue 实现动态路由的方法 在 Vue 中实现动态路由通常涉及以下几个关键步骤: 使用 Vue Router 的动态路由匹配 通过 Vue Router 的 :param 语法可以实现动态路由匹配。…