Vue 封装 Switch开关组件

封装switch 主要内容包括基础应用、实用技巧、原理机制等方面
直接上代码

子组件 /child.vue

  • html
<template>
  <label role="checkbox" :class="['switch', { toggled }]">
    <input type="checkbox" class="switch-input" @change="toggle" />
    <div
      class="switch-core"
      :style="{
        backgroundColor: toggled ? colorStart : colorStop,
      }"
    >
      <div
        class="switch-button"
        :style="{
          transition: `transform ${speed}ms`,
          transform: toggled ? null : `translate3d(32px, 0px, 0px)`,
        }"
      ></div>
    </div>
    <span class="switch-label label-right" v-if="toggled" v-html="labelStart">
    </span>
    <span class="switch-label label-left" v-html="labelStop" v-else> </span>
  </label>
</template>
export default {
  name: "ToggleSwitch",
  data() {
    return { toggled: this.value };
  },

  props: {
    value: { type: Boolean, default: true }, //默认为启用
    speed: { type: Number, default: 100 }, //切换过度动画
    labelStart: { type: String, default: "启用" },
    labelStop: { type: String, default: "停用" },
    colorStart: { type: String, default: "#11CED2" },
    colorStop: { type: String, default: "#f44336" },
  },
  watch: {
    //利用watch监听改变值  将子级状态传给父级
    value: function (val) {
      this.value = val;
    },
  },
  methods: {
    toggle(event) {
      this.toggled = !this.toggled;
      this.$emit("update:value", this.toggled);
      this.$emit("change", event); //将方法抛出去
    },
  },
};

<style lang="scss" scoped>
.switch {
  margin: 20px;
  display: inline-block;
  position: relative;
  overflow: hidden;
  vertical-align: middle;
  user-select: none;
  font-size: 10px;
  cursor: pointer;
  .switch-input {
    display: none;
  }
  .switch-label {
    position: absolute;
    top: 0;
    font-weight: 600;
    color: white;
    z-index: 2;
    &.label-left {
      left: 10px;
      line-height: 20px;
      border-top-left-radius: 2px;
      border-bottom-left-radius: 2px;
      color: #fff;
    }
    &.label-right {
      right: 10px;
      line-height: 20px;
      border-top-right-radius: 2px;
      border-bottom-right-radius: 2px;
    }
  }
  .switch-core {
    display: block;
    position: relative;
    box-sizing: border-box;
    outline: 0;
    margin: 0;
    transition: border-color 0.3s, background-color 0.3s;
    user-select: none;
    width: 46px;
    height: 20px;
    border-radius: 4px;
    line-height: 20px;
    .switch-button {
      width: 8px;
      height: 16px;
      display: block;
      position: absolute;
      overflow: hidden;
      top: 2;
      left: 2;
      z-index: 3;
      transform: translate3d(0, 0, 0);
      background-color: rgba(255, 255, 255, 1);
      border-radius: 4px;
      margin-top: 2px;
      margin-left: 2px;
    }
  }
}
</style>

父组件调用

<template>
  <div>
  <toggle-switch v-model="value" :value.sync="value" @change="Change" > </toggle-switch>
  </div>
</template>
<script>
import ToggleSwitch from "/child";
export default {
  data() {
    return {
    value:true
    };
  },
  methods: {
  Change() { console.log("switch值改变"); }
  },
  components:{ "toggle-switch":ToggleSwitch }
};
</script>
</script>

在这里插入图片描述
在这里插入图片描述

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值