当前位置:首页 > 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 实现走势图的方法 使用 ECharts 库 ECharts 是一个强大的数据可视化库,支持多种图表类型,包括走势图(折线图)。在 Vue 项目中可以通过 vue-echarts 封装库或直接使…

vue实现布局

vue实现布局

Vue 实现布局方法 Vue 提供了多种方式实现页面布局,可以通过组件化、CSS 框架或自定义样式完成。以下是几种常见方法: 使用 CSS Flexbox 或 Grid 通过 Vue 单文件组件结合…

vue 实现modal

vue 实现modal

Vue 实现 Modal 的方法 使用 Vue 原生组件 通过 Vue 的组件系统可以轻松实现 Modal。创建一个可复用的 Modal 组件,通过 props 控制显示与隐藏。 <templ…

vue实现视频

vue实现视频

Vue 实现视频播放的方法 在 Vue 项目中实现视频播放可以通过多种方式完成,以下是几种常见的方法: 使用 HTML5 的 <video> 标签 在 Vue 组件中直接使用 HTML…

vue实现表单

vue实现表单

Vue 表单实现方法 双向数据绑定 使用 v-model 指令实现表单元素与数据的双向绑定。适用于 input、textarea、select 等元素。 <template> <…

vue实现拖拉

vue实现拖拉

实现拖拽功能的基本步骤 在Vue中实现拖拽功能可以通过原生HTML5的拖放API或第三方库如vuedraggable完成。以下是两种方法的详细说明。 使用HTML5原生拖放API HTML5提供了原…