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

使用自定义组件

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

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

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

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

vue实现switch开关

标签: vueswitch
分享给朋友:

相关文章

vue评分实现

vue评分实现

Vue 评分组件实现方法 使用第三方库(如 Element UI) 安装 Element UI: npm install element-ui 引入并注册组件: import Vue from…

vue实现图册

vue实现图册

Vue实现图册功能 在Vue中实现图册功能通常涉及图片展示、切换、缩放等交互效果。以下是常见的实现方法和关键代码示例: 基础图册组件结构 使用Vue单文件组件构建基础结构: <templat…

vue 实现列表

vue 实现列表

Vue 实现列表的方法 在 Vue 中实现列表通常使用 v-for 指令,结合数组或对象数据渲染动态列表。以下是几种常见的实现方式: 基础列表渲染 使用 v-for 指令遍历数组,渲染列表项。ite…

vue实现tree

vue实现tree

Vue 实现 Tree 组件 使用 Vue 实现 Tree 组件可以通过递归组件的方式来实现层级结构展示。以下是一个完整的实现方法: 基础递归组件实现 创建 Tree 组件文件 Tree.vue,…

vue 实现跳转

vue 实现跳转

Vue 实现页面跳转的方法 在 Vue 中实现页面跳转通常可以通过以下几种方式实现,具体选择取决于项目需求和技术栈(如是否使用 Vue Router)。 使用 Vue Router 进行编程式导航…

vue实现 弹窗

vue实现 弹窗

Vue 实现弹窗的基本方法 使用 Vue 实现弹窗可以通过多种方式,包括组件化、指令、插件等。以下是几种常见的实现方法。 组件化实现弹窗 创建一个独立的弹窗组件,通过 props 控制显示与隐藏。…