vue switch组件

本文介绍了如何使用Vue.js结合自定义的kl-switch组件,通过v-model绑定数据和@change事件监听器,实现在两个预设选项间切换,并根据状态动态改变模板样式。还展示了如何根据用户输入实时更新组件状态和触发回调函数。
摘要由CSDN通过智能技术生成

示例

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

使用

  <kl-switch :default="switchRule" v-model="isOk" @change="change"></kl-switch>
 
 data() {
        return {
            isOk: '1',
            switchRule: ['1', '2'],
        }
    },
      change(val) {
            console.log(val)
        },

实现

<template>
    <div
        :class="['kl-switch', disabled ? 'is-disabled' : '']"
        :style="{
            backgroundColor: isOk ? activeColor : inactiveColor,
            transform: `scale(${ratio})`,
        }"
        @click="switchVal"
    >
        <div class="kl-switch-radio" :style="{ left: isOk ? '1px' : '22px' }"></div>
    </div>
</template>

<script>
export default {
    props: {
        ratio: {
            type: Number,
            default: 1,
        },
        activeColor: {
            type: String,
            default: '#13ce66',
        },
        inactiveColor: {
            type: String,
            default: '#ff4949',
        },
        disabled: {
            type: Boolean,
            default: false,
        },
        default: {
            type: Array,
            default: [],
        },
        value: {
            type: String | Number | Boolean,
            require: true,
        },
    },
    data() {
        return {
            isOk: null,
            rules: [],
        }
    },
    watch: {
        value: {
            handler(val) {
                // 以下方式只需要在初始化执行即可
                if (this.default.length === 2 && this.default[0] !== this.default[1]) {
                    this.rules = JSON.parse(JSON.stringify(this.default))
                    let index = this.rules.findIndex((item) => item === val)
                    switch (index) {
                        case -1:
                            this.rules = [true, false]
                            break
                        case 0:
                            this.isOk = true
                            break
                        case 1:
                            this.isOk = false
                            break
                    }
                } else {
                    this.rules = [true, false]
                    this.value ? (this.isOk = true) : (this.isOk = false)
                }
            },
            immediate: true,
            deep: true,
        },
    },
    methods: {
        switchVal() {
            if (this.disabled) {
                return
            }
            this.isOk = !this.isOk
            this.$emit('input', this.isOk ? this.rules[0] : this.rules[1])
            this.$emit('change', this.isOk ? this.rules[0] : this.rules[1])
        },
    },
}
</script>

<style lang="scss" scoped>
.kl-switch {
    cursor: pointer;
    width: 40px;
    height: 20px;
    border-radius: 10px;
    position: relative;

    .kl-switch-radio {
        position: absolute;
        top: 50%;
        left: 1px;
        transform: translate(0, -50%);
        width: 16px;
        height: 16px;
        background-color: #fff;
        border-radius: 50%;
        transition: left 0.3s;
    }
}
.is-disabled {
    opacity: 0.6;
    cursor: not-allowed;
}
</style>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值