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

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

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

vue switch开关实现

标签: vueswitch
分享给朋友:

相关文章

vue实现点击样式

vue实现点击样式

Vue 实现点击样式的方法 在 Vue 中实现点击样式可以通过多种方式完成,以下是几种常见的方法: 方法一:使用 v-bind:class 动态绑定类名 通过数据驱动的方式动态切换类名,结合 CS…

vue实现sql操作

vue实现sql操作

在Vue中实现SQL操作通常需要结合后端服务或本地数据库技术,因为前端JavaScript无法直接连接SQL数据库。以下是几种常见的实现方式: 通过API与后端交互 Vue前端通过HTTP请求(如a…

vue实现压缩上传文件

vue实现压缩上传文件

压缩上传文件的实现方法 在Vue中实现文件压缩和上传功能,可以通过以下步骤完成。该方法结合了前端文件压缩库和HTTP请求,确保文件在上传前被有效压缩。 安装必要的依赖 需要使用compressor…

vue实现横向导航

vue实现横向导航

vue实现横向导航的方法 使用Flex布局实现横向导航 在Vue中可以通过Flex布局快速实现横向导航。创建一个组件,使用display: flex和flex-direction: row属性使导航项…

vue实现导航切换内容

vue实现导航切换内容

Vue实现导航切换内容的方法 在Vue中实现导航切换内容,可以通过以下方式实现: 使用动态组件 动态组件通过<component :is="currentComponent">实现内容…

vue按钮实现截屏

vue按钮实现截屏

Vue 按钮实现截屏的方法 在 Vue 项目中实现截屏功能可以通过多种方式完成,以下是几种常见的方法: 使用 html2canvas 库 安装 html2canvas 库: npm insta…