当前位置:首页 > VUE

vue实现switch开关

2026-02-17 23:39:05VUE

Vue 实现 Switch 开关

使用原生 HTML 和 CSS

可以通过原生的 HTML 复选框结合 CSS 样式实现 Switch 开关效果。

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

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

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

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

.slider.round {
  border-radius: 34px;
}

.slider.round:before {
  border-radius: 50%;
}
</style>

使用第三方组件库

Vue 生态中有许多成熟的 UI 组件库提供了 Switch 开关组件,可以直接使用。

Element UI

<template>
  <el-switch v-model="isChecked"></el-switch>
</template>

<script>
export default {
  data() {
    return {
      isChecked: false
    }
  }
}
</script>

Ant Design Vue

<template>
  <a-switch v-model:checked="isChecked" />
</template>

<script>
export default {
  data() {
    return {
      isChecked: false
    }
  }
}
</script>

自定义 Switch 组件

可以封装一个可复用的 Switch 组件,方便在项目中多次调用。

Switch.vue

<template>
  <label class="custom-switch">
    <input 
      type="checkbox" 
      v-model="modelValue" 
      @change="$emit('update:modelValue', $event.target.checked)"
    >
    <span class="custom-slider"></span>
  </label>
</template>

<script>
export default {
  name: 'CustomSwitch',
  props: {
    modelValue: {
      type: Boolean,
      default: false
    }
  }
}
</script>

<style>
.custom-switch {
  position: relative;
  display: inline-block;
  width: 50px;
  height: 24px;
}

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

.custom-slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  transition: .4s;
  border-radius: 24px;
}

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

input:checked + .custom-slider {
  background-color: #42b983;
}

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

使用自定义组件

vue实现switch开关

<template>
  <CustomSwitch v-model="isActive" />
</template>

<script>
import CustomSwitch from './Switch.vue'

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

以上方法可以根据项目需求选择适合的方式实现 Switch 开关功能。

标签: vueswitch
分享给朋友:

相关文章

vue实现前端注册

vue实现前端注册

Vue 实现前端注册功能 注册表单设计 使用 Vue 的模板语法创建注册表单,包含用户名、邮箱、密码和确认密码字段。表单需绑定 v-model 实现双向数据绑定。 <template>…

vue实现继承语法糖

vue实现继承语法糖

在Vue中实现继承可以通过多种方式模拟类继承的效果,以下是常见的几种方法: 使用mixins实现逻辑复用 通过Vue的mixins机制共享组件选项,达到类似继承的效果: const BaseCom…

vue实现影院

vue实现影院

Vue实现影院功能的基本思路 使用Vue框架开发影院功能需要结合前端组件化开发、状态管理以及后端API交互。以下是关键实现步骤: 影院页面布局设计 影院页面通常分为顶部导航栏、影院列表区域和底部导航…

vue实现幻灯

vue实现幻灯

Vue实现幻灯片的基础方法 使用Vue实现幻灯片功能可以通过多种方式完成,以下是几种常见方案: 方案一:基于Vue的过渡动画 通过Vue的<transition>或<transit…

vue实现单击

vue实现单击

Vue 实现单击事件的方法 在 Vue 中实现单击事件可以通过 v-on 指令或简写的 @ 符号绑定事件处理器。以下是几种常见的实现方式: 基础用法 通过 v-on:click 或 @click 绑…

vue实现滑动条

vue实现滑动条

Vue 实现滑动条 在 Vue 中实现滑动条可以通过原生 HTML 的 <input type="range"> 或自定义组件实现。以下是两种常见方法: 使用原生 HTML 滑动条 通过…