当前位置:首页 > VUE

vue实现switch组件

2026-01-19 03:12:07VUE

Vue 实现 Switch 组件

基本结构

使用 Vue 的单文件组件(SFC)实现一个基础的 Switch 组件。模板部分包含一个 div 包裹的 inputspan,通过 CSS 实现开关样式。

<template>
  <div class="switch" @click="toggle">
    <input type="checkbox" v-model="isChecked" class="switch-input" />
    <span class="slider"></span>
  </div>
</template>

数据与事件

通过 v-model 实现双向绑定,监听点击事件切换状态。

<script>
export default {
  props: {
    value: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      isChecked: this.value
    };
  },
  methods: {
    toggle() {
      this.isChecked = !this.isChecked;
      this.$emit('input', this.isChecked);
      this.$emit('change', this.isChecked);
    }
  },
  watch: {
    value(newVal) {
      this.isChecked = newVal;
    }
  }
};
</script>

样式设计

使用 CSS 实现滑动效果,通过伪类和过渡动画增强交互体验。

<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: 0.4s;
  border-radius: 34px;
}

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

.switch-input:checked + .slider {
  background-color: #4CAF50;
}

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

使用示例

在父组件中通过 v-model 绑定数据,监听状态变化。

<template>
  <div>
    <Switch v-model="isActive" @change="handleChange" />
  </div>
</template>

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

export default {
  components: { Switch },
  data() {
    return {
      isActive: false
    };
  },
  methods: {
    handleChange(val) {
      console.log('Switch state:', val);
    }
  }
};
</script>

自定义样式

通过 props 支持自定义颜色和尺寸,增强组件灵活性。

props: {
  activeColor: {
    type: String,
    default: '#4CAF50'
  },
  inactiveColor: {
    type: String,
    default: '#ccc'
  },
  size: {
    type: String,
    default: 'medium',
    validator: (val) => ['small', 'medium', 'large'].includes(val)
  }
}

通过计算属性动态生成样式类:

vue实现switch组件

computed: {
  sliderStyle() {
    return {
      backgroundColor: this.isChecked ? this.activeColor : this.inactiveColor,
      width: this.size === 'small' ? '40px' : this.size === 'large' ? '80px' : '60px',
      height: this.size === 'small' ? '20px' : this.size === 'large' ? '40px' : '34px'
    };
  }
}

标签: 组件vue
分享给朋友:

相关文章

vue实现好评弹框

vue实现好评弹框

Vue 实现好评弹框的方法 使用 Vue 实现好评弹框可以通过组件化方式完成,结合动态数据绑定和事件处理。以下是具体实现步骤: 1. 创建弹框组件 新建一个 RatingDialog.vue 组件…

vue实现https

vue实现https

Vue 中实现 HTTPS 的步骤 在 Vue 项目中实现 HTTPS 主要涉及开发环境配置和生产环境部署。以下是具体方法: 开发环境配置(Vue CLI 或 Vite) 使用 mkcert 生成…

vue实现 toast

vue实现 toast

Vue 实现 Toast 组件 在 Vue 中实现 Toast 组件可以通过多种方式完成,以下是几种常见的方法: 使用全局组件和事件总线 创建一个全局的 Toast 组件,并通过事件总线或 Vue…

vue实现fullpage

vue实现fullpage

Vue 实现 FullPage 效果 在 Vue 中实现全屏滚动(FullPage)效果可以通过多种方式完成,以下是几种常见的方法: 使用现有库 vue-fullpage.js 安装 vue-ful…

vue实现弹幕

vue实现弹幕

Vue 实现弹幕功能 弹幕功能通常包括动态生成、滚动显示、颜色和速度随机化等特性。以下是基于 Vue 3 的实现方法。 核心思路 使用 CSS 动画控制弹幕从右向左滚动。 动态生成弹幕数据,随机设置…

react实现vue

react实现vue

React 实现 Vue 功能 React 和 Vue 是两种不同的前端框架,但可以通过一些方法在 React 中实现 Vue 的特性。以下是几种常见 Vue 功能在 React 中的实现方式: 双…