当前位置:首页 > VUE

vue实现开关

2026-01-13 00:05:23VUE

Vue 实现开关组件的方法

使用原生 HTML 和 Vue 数据绑定

通过 v-model 绑定复选框的选中状态,利用 CSS 样式模拟开关效果。

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

使用第三方 UI 库

Element UI 提供了现成的开关组件:

<template>
  <el-switch v-model="value" active-color="#13ce66" inactive-color="#ff4949">
  </el-switch>
</template>

<script>
export default {
  data() {
    return {
      value: true
    }
  }
}
</script>

Ant Design Vue 的开关组件:

vue实现开关

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

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

自定义可复用的开关组件

创建可复用的开关组件,支持自定义样式和事件:

<!-- Switch.vue -->
<template>
  <label class="custom-switch">
    <input 
      type="checkbox" 
      :checked="modelValue" 
      @change="$emit('update:modelValue', $event.target.checked)"
    >
    <span class="custom-slider" :class="{ 'active': modelValue }"></span>
    <span class="label">{{ label }}</span>
  </label>
</template>

<script>
export default {
  props: {
    modelValue: Boolean,
    label: String
  },
  emits: ['update:modelValue']
}
</script>

<style>
/* 自定义样式 */
</style>

使用自定义组件:

vue实现开关

<template>
  <Switch v-model="isActive" label="启用功能" />
</template>

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

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

动画效果增强

为开关添加更流畅的动画效果:

.slider {
  /* 原有样式 */
  box-shadow: 0 0 1px rgba(0,0,0,0.3);
}

.slider:before {
  /* 原有样式 */
  box-shadow: 0 2px 5px rgba(0,0,0,0.2);
}

input:checked + .slider {
  /* 原有样式 */
  box-shadow: 0 0 1px #2196F3;
}

无障碍支持

增加 ARIA 属性提升可访问性:

<label class="switch">
  <input 
    type="checkbox" 
    v-model="isChecked"
    role="switch"
    :aria-checked="isChecked"
    :aria-label="isChecked ? '开启' : '关闭'"
  >
  <span class="slider"></span>
</label>

标签: vue
分享给朋友:

相关文章

vue实现iframe

vue实现iframe

Vue 中实现 iframe 的方法 在 Vue 项目中,可以通过多种方式实现 iframe 的嵌入。以下是几种常见的方法: 使用原生 HTML iframe 标签 直接在 Vue 组件的模板中使用…

vue实现全景

vue实现全景

Vue 实现全景效果的方法 使用 Vue 实现全景效果通常需要结合第三方库或框架,以下是几种常见的方法: 使用 Three.js 和 Vue-Three.js Three.js 是一个强大的 3D…

vue实现通讯

vue实现通讯

Vue 组件通讯方法 父子组件通讯 父组件向子组件传递数据通过 props,子组件向父组件传递数据通过 $emit 事件。 父组件模板: <child-component :message=…

lua 实现vue

lua 实现vue

在 Lua 中实现类似 Vue 的响应式框架需要模拟 Vue 的核心功能,包括数据绑定、模板渲染和依赖追踪。以下是实现的关键思路和代码示例: 响应式数据绑定 使用 Lua 的 __index 和 _…

vue实现气泡

vue实现气泡

Vue 实现气泡效果的方法 在 Vue 中实现气泡效果可以通过 CSS 动画、第三方库或自定义组件完成。以下是几种常见实现方式: 使用纯 CSS 和 Vue 过渡 通过 Vue 的过渡系统结合 CS…

vue实现登录权限

vue实现登录权限

Vue 实现登录权限方案 路由守卫控制访问权限 安装vue-router并配置路由守卫,在全局前置守卫中检查用户登录状态和权限。未登录用户访问受限路由时重定向到登录页。 // router.js r…