我是靠谱客的博主 疯狂时光,最近开发中收集的这篇文章主要介绍vue3-switch组件封装,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

<template>
  <div
    :class="switchClass"
    :disabled="$props.disabled"
    @click.prevent="switchValue"
  >
    <div class="b-switch-round"></div>
  </div>
</template>

<script lang="ts">
import { defineComponent, computed, toRefs } from 'vue'
import type { PropType } from 'vue'
import type { checkedType } from './interface'

export default defineComponent({
  name: 'BSwitch',
  props: {
    checked: {
      type: Boolean as PropType<checkedType>,
      default: false
    },
    disabled: {
      type: Boolean as PropType<boolean>,
      default: false
    }
  },
  emits: ['change', 'update:checked'],
  setup(props, { emit }) {
    const { checked, disabled } = toRefs(props)
    const switchClass = computed(() => [
      'b-switch',
      checked.value && 'b-switch-active',
      disabled.value && checked.value && 'b-switch-opendisabled',
      disabled.value && !checked.value && 'b-switch-closedisabled'
    ])
    const switchValue = () => {
      if (disabled.value) return
      emit('update:checked', !checked.value)
    }
    return { switchClass, switchValue }
  }
})
</script>

<style scoped lang="less">
@import "../style/switch.less";
</style>

css部分:

.b-switch {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  width: 36px;
  height: 10px;
  cursor: pointer;
  background-color: @border;
  border-radius: 6px;
  transition: all 0.3s ease;

  &-round {
    width: 16px !important;
    height: 16px;
    background: @backgroundColor;
    border-radius: 50%;
    box-shadow: 0 1px 6px 0 rgb(75 88 122 / 20%);
    transition: all 0.3s ease 0s;
  }
}

.b-switch-active {
  background: @successColor;

  .b-switch-round {
    margin-left: 20px;
  }
}

.b-switch-opendisabled {
  cursor: not-allowed;
  background: rgb(46 212 169 / 30%);
}

.b-switch-closedisabled {
  cursor: not-allowed;
  background: rgb(225 228 232 / 50%);
}

最后

以上就是疯狂时光为你收集整理的vue3-switch组件封装的全部内容,希望文章能够帮你解决vue3-switch组件封装所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(68)

评论列表共有 0 条评论

立即
投稿
返回
顶部