Vue2开关(Switch)

39 篇文章 5 订阅

Vue3开关(Switch)

可自定义设置以下属性:

  • 初始是否选中(defaultChecked),默认 false

  • 选择时的内容(checkedInfo),默认 null

  • 未选中时的内容(uncheckedInfo),默认 null

  • 是否禁用(disabled),默认 false

  • (v-model)指定当前是否选中(checked),默认 null

效果如下图:

①创建开关组件Switch.vue:

<template>
  <div class="m-switch-wrap">
    <div @click="disabled ? e => e.preventDefault() : onSwitch()" :class="['m-switch', { 'switch-checked': checkedVal, 'disabled': disabled }]">
      <div :class="['u-switch-inner', checkedVal ? 'inner-checked' : 'inner-unchecked' ]">{{ checkedVal ? checkedInfo : uncheckedInfo }}</div>
      <div :class="['u-node', { 'node-checked': checkedVal }]"></div>
    </div>
  </div>
</template>
<script>
export default {
  name: 'Switch',
  model: {
    prop: 'checked',
    event: 'change'
  },
  props: {
    defaultChecked: { // 初始是否选中
      type: Boolean,
      default: false
    },
    checkedInfo: { // 选中时的内容
      type: [Number, String],
      default: null
    },
    uncheckedInfo: { // 未选中时的内容
      type: [Number, String],
      default: null
    },
    disabled: { // 是否禁用
      type: Boolean,
      default: false
    },
    checked: { // (v-model)指定当前是否选中
      type: Boolean,
      default: null
    }
  },
  data () {
    return {
      checkedVal: null
    }
  },
  watch: {
    checked () {
      this.initSwitcher()
    },
    defaultChecked () {
      this.initSwitcher()
    }
  },
  created () {
    this.initSwitcher()
  },
  methods: {
    initSwitcher () {
      if (typeof this.checked === 'boolean') {
        this.checkedVal = this.checked
      } else if (typeof this.checked === 'object') {
        this.checkedVal = this.defaultChecked
      }
    },
    onSwitch () {
      this.checkedVal = !this.checkedVal
      this.$emit('change', this.checkedVal)
    }
  }
}
</script>
<style lang="less" scoped>
@themeColor: #1890FF;
.m-switch-wrap {
  height: 22px;
  min-width: 44px;
  display: inline-block;
  .m-switch {
    position: relative;
    height: 22px;
    color: rgba(0,0,0,.65);
    font-size: 14px;
    background: rgba(0,0,0,.25);
    border-radius: 100px;
    cursor: pointer;
    transition: background .36s;
    .u-switch-inner {
      display: inline-block;
      color: #fff;
      font-size: 14px;
      line-height: 22px;
      padding: 0 8px;
      transition: all .36s;
    }
    .inner-checked {
      margin-right: 18px;
    }
    .inner-unchecked {
      margin-left: 18px;
    }
    .u-node {
      position: absolute;
      top: 2px;
      left: 2px;
      width: 18px;
      height: 18px;
      background: #FFF;
      border-radius: 100%;
      cursor: pointer;
      transition: all .36s;
    }
    .node-checked { // 结果等价于right: 2px; 为了滑动效果都以左边为基准进行偏移
      left: 100%;
      margin-left: -2px;
      transform: translateX(-100%);
    }
  }
  .switch-checked {
    background: @themeColor;
  }
  .disabled {
    cursor: not-allowed;
    opacity: .4;
  }
}
</style>

②在要使用的页面引入组件:

<Switch :defaultChecked="true" v-model="checked" checkedInfo="开" uncheckedInfo="关" :disabled="false" />
import Switch from '@/components/Switch'
export default {
	name: 'Index',
	components: {
		Switch
	},
    data () {
        return {
             checked: true
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值