封装一个switch开关组件

<template>
  <div class="switch" @click="openSwitch">
    <div class="switch-false" :class="{ on: switchStatus === true }"></div>
    <div class="switch-true" ref="switchTrue"></div>
    <div class="switch-btn" :class="{ on: switchStatus === true }"></div>
  </div>
</template>

<script>
export default {
  props: {
    status: {
      type: Boolean
    },
    width: {
      type: Number,
      default: 50
    },
    height: {
      type: Number,
      default: 20
    },
    activeColor: {
      type: String,
      default: '#13ce66'
    },
    inactiveColor: {
      type: String,
      default: '#ff4949'
    },
    activeBorderColor: {
      type: String,
      default: '#13ce66'
    },
    inactiveBorderColor: {
      type: String,
      default: '#ff4949'
    }
  },
  data() {
    return {
      switchStatus: false
    }
  },
  methods: {
    openSwitch() {
      this.switchStatus = !this.switchStatus
      if (this.switchStatus) {
        this.$refs.switchTrue.classList.value = 'switch-true on'
      } else {
        this.$refs.switchTrue.classList.value = 'switch-true off'
      }
      this.$emit('change', this.switchStatus)
    }
  },
  mounted() {
    this.$el.style.setProperty('--width', this.width + 'px')
    this.$el.style.setProperty('--width2', this.width / 2 + 'px')
    this.$el.style.setProperty('--height', this.height + 'px')
    this.$el.style.setProperty('--activeColor', this.activeColor)
    this.$el.style.setProperty('--inactiveColor', this.inactiveColor)
    this.$el.style.setProperty('--activeBorderColor', this.activeBorderColor)
    this.$el.style.setProperty('--inactiveBorderColor', this.inactiveBorderColor)
  },
  watch: {
    status: {
      handler(val) {
        this.switchStatus = val
      },
      deep: true,
      immediate: true
    }
  }
}
</script>

<style scoped>
.switch {
  width: var(--width);
  height: var(--height);
  position: relative;
  cursor: pointer;
}
@keyframes moveOn {
  0% {
    border: none;
    width: 0px;
  }
  1% {
    border: 1px solid var(--activeBorderColor);
  }
  100% {
    border: 1px solid var(--activeBorderColor);
    width: var(--width);
  }
}
@keyframes moveOff {
  0% {
    border: 1px solid var(--activeBorderColor);
    width: var(--width);
  }
  99% {
    border: 1px solid var(--activeBorderColor);
  }
  100% {
    border: none;
    width: 0px;
  }
}
.switch-false {
  width: 100%;
  height: 100%;
  background: var(--inactiveColor);
  border-radius: 3px;
  border: 1px solid var(--inactiveBorderColor);
  position: absolute;
  top: 0;
  right: 0;
  transition: width 0.5s;
}
.switch-true {
  width: 0px;
  height: 100%;
  background: var(--activeColor);
  border-radius: 3px;
  position: absolute;
  top: 0;
  left: 0;
  z-index: 2;
}
.switch-btn {
  width: var(--width2);
  height: var(--height);
  background: #ffffff;
  border-radius: 3px;
  position: absolute;
  top: 0px;
  left: 0px;
  transition: left .5s;
  z-index: 3;
}
.switch-false.on {
  width: 0px;
}
.switch-true.on {
  border: 1px solid var(--activeBorderColor);
  width: 100%;
  animation: moveOn .5s ease;
}

.switch-true.off {
  animation: moveOff .5s ease;
}

.switch-btn.on {
  /*background: var(--activeColor);*/
  left: var(--width2);
}
</style>

应用

<template>
	<switch :status="false" :width="50" :height="20" active-color="#13ce66" inactive-color="#ff4949" active-border-color="#13ce66" inactive-border-color="#ff4949" @change="changeSwitch"></switch>
</template>
<script>
import Switch from '@/components/switch'
export default {
  components: {
    Switch
  },
  methods: {
    changeSwitch(val) {
      console.log(val)
    }
  }
}
</script>

说明:

参数说明类型默认值
status开始状态Booleanfalse
width宽度Number50
hight高度Number20
active-colorswitch打开时的背景色String#13ce66
inactive-colorswitch关闭时的背景色String#ff4949
active-border-colorswitch打开时的边框色String#13ce66
inactive-border-colorswitch关闭时的边框色String#ff4949

效果图:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值