当前位置:首页 > 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 中实现换行可以通过多种方式,具体取决于使用场景和需求。以下是常见的几种方法: 使用 HTML 的 <br> 标签 在模板中直接使用 <br…

vue jwt实现

vue jwt实现

Vue JWT 实现方法 安装依赖 确保项目中安装了 jsonwebtoken(后端)和 axios(前端)。若使用 Vue 3,可搭配 vue-router 和 pinia(或 vuex)管理状态。…

vue实现popper

vue实现popper

Vue 实现 Popper 的方法 使用 Tippy.js 库 Tippy.js 是一个轻量级的 Popper.js 封装库,提供丰富的工具提示功能。安装 Tippy.js 及其 Vue 封装: n…

vue 实现聊天

vue 实现聊天

使用 Vue 实现聊天功能 创建 Vue 项目并安装依赖 确保已安装 Vue CLI,通过以下命令创建新项目: vue create chat-app 进入项目目录后,安装必要的依赖(如 Socke…

vue博客实现

vue博客实现

Vue 博客实现步骤 项目初始化与基础配置 使用 Vue CLI 或 Vite 创建项目,安装基础依赖如 vue-router 和状态管理库(如 pinia)。配置路由文件实现页面跳转逻辑,例如博客首…

vue实现颜色闪烁

vue实现颜色闪烁

实现颜色闪烁的方法 在Vue中实现颜色闪烁效果可以通过CSS动画或JavaScript定时器动态修改样式。以下是两种常见的实现方式: 使用CSS动画实现 通过定义@keyframes动画规则…