当前位置:首页 > VUE

vue switch开关实现

2026-01-15 22:16:05VUE

使用 Vue 实现 Switch 开关组件

基础实现

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

vue 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 封装为可复用组件,支持自定义颜色和尺寸:

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

标签: vueswitch
分享给朋友:

相关文章

vue实现屏幕共享

vue实现屏幕共享

Vue 实现屏幕共享的方法 使用 WebRTC 和 getDisplayMedia API 通过 navigator.mediaDevices.getDisplayMedia 捕获屏幕内容,结合 We…

vue手动实现弹窗

vue手动实现弹窗

实现弹窗组件的基本结构 在Vue中手动实现弹窗组件,需要创建一个独立的弹窗组件,并通过props控制其显示与隐藏。弹窗组件通常包含遮罩层、内容区域和关闭按钮。 <template>…

vue实现走势图

vue实现走势图

Vue 实现走势图的方法 使用 ECharts 库 ECharts 是一个强大的数据可视化库,支持多种图表类型,包括走势图(折线图)。在 Vue 项目中可以通过 vue-echarts 封装库或直接使…

vue实现3d宇宙

vue实现3d宇宙

Vue实现3D宇宙效果 使用Three.js库 Three.js是一个强大的JavaScript 3D库,可以轻松集成到Vue项目中。安装Three.js依赖: npm install three…

vue实现菜单栏锚点

vue实现菜单栏锚点

实现锚点菜单的基本思路 在Vue中实现菜单栏锚点功能,主要涉及两个方面:创建可跳转的锚点位置,以及菜单项的点击滚动控制。通过监听滚动事件可以高亮当前可见区域的对应菜单项。 创建页面锚点位置 在需要跳…

vue实现a4打印模板

vue实现a4打印模板

使用Vue实现A4打印模板 在Vue中实现A4打印模板需要结合CSS的打印样式和Vue的模板渲染能力。以下是具体实现方法: 设置A4纸张尺寸 在CSS中定义A4纸张的标准尺寸(210mm × 29…