当前位置:首页 > 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中实现手写签名功能,通常需要借助HTML5的Canvas API来捕获用户的绘制操作。核心流程包括初始化画布、监听触摸/鼠标事件、记录轨迹以及生成签名图片。 安装依赖…

vue 实现

vue 实现

Vue 实现的基本步骤 安装 Vue.js 可以通过 npm 或 CDN 引入。使用 npm 安装适合大型项目,CDN 适合快速原型开发。 npm install vue 在 HTML 文件中引入…

vue实现导出

vue实现导出

Vue 实现导出功能的方法 在 Vue 项目中实现导出功能通常涉及将数据导出为 Excel、CSV 或其他格式。以下是几种常见的实现方式: 使用 xlsx 库导出 Excel 安装 xlsx 库:…

vue多选实现

vue多选实现

Vue 多选实现方法 基础实现(v-model + multiple) 在 Vue 中实现多选可以通过 <select> 元素的 multiple 属性结合 v-model 完成。v-mo…

vue环境实现

vue环境实现

Vue 环境搭建 安装 Node.js 和 npm Node.js 是运行 Vue 的基础环境,npm 是包管理工具。从 Node.js 官网 下载并安装最新稳定版本。安装完成后,在终端运行以下命令验…

vue实现伸缩

vue实现伸缩

Vue实现伸缩功能的方法 使用CSS过渡和Vue的v-if或v-show 通过CSS的transition属性结合Vue的条件渲染指令(v-if或v-show)可以实现元素的伸缩效果。定义一个CSS类…