Vue Input输入框两侧加减框内数字组件

NumberBox组件内容:

<template>
    <div class="number-box" :class="{'box-disable': isDisabled}">
        <!-- 此处为“减号”标志的图片 -->
        <img class="btn-number" src="./reduce.png" @click="onReduce" />
        <div class="number-container">
            <Input v-model="strVal" @on-enter="onEnter" :disabled="isDisabled" @on-blur="onBlur" placeholeder="请输入范围" />
            <div class="unit" v-text="米" />
        </div>
        <img class="btn-number" src="./recrease.png" @click="onIncrease" />
    </div>
</template>

<script>
    export default {
        props: {
            isDisabled: {
                type: Boolean,
                default: false
            },
            curVal: {
                type: Number,
                default: 50
            },
            minVal: {
                type: Number,
                default: 50
            },
            maxVal: {
                type: Number,
                default: 1000
            }
        },
        data() {
            return {
                numVal: this.curVal,
                strVal: this.curVal.toString()
            }
        },
        methods: {
            onEnter() {
                if(Number(this.strVal) <= this.minVal) {
                    this.strVal = this.minVal.toString();
                    this.numVal = this.minVal;
                }
                if(Number(this.strVal) >= this.maxVal) {
                    this.strVal = this.maxVal.toString();
                    this.numVal = this.maxVal;
                }
                
                this.numVal = Number(this.strVal);
                this.$emit("getNumber", this.numVal);
            },
            onBlur() {
                this.nextTick(() => {
                    this.onEnter();
                });
            },
             onReduce() {
                if(Number(this.strVal) > this.minVal) {
                    this.strVal = Number(this.strVal);
                    this.numVal -= 1;
                    this.strVal = this.numVal.toString();
                    this.$emit("getNumber", this.numVal);
                }
            },
            onIncrease() {
                if(Number(this.strVal) < this.maxVal) {
                    this.strVal = Number(this.strVal);
                    this.numVal += 1;
                    this.strVal = this.numVal.toString();
                    this.$emit("getNumber", this.numVal);
                }
            },
            resetCurVal(val) {
                this.strVal = val.toString();
                this.numVal = val;
                this.$emit("getNumber", this.numVal);
            }
        }
    }
</script>

<style lang="less" scoped>
    /*数字输入框*/
    .box-disable {
        pointer-events: none;
        color: #686868 !important;
    }
    .number-box {
        color: #FFF;
        dispaly: felx;
        flex-flow: row nowrap;
        justify-content: space-between;
        .btn-number {
            cursor: pointer;
            margin-top: 7px;
            width: 15px;
            height: 15px;
        }
        .number-container {
            display: flex;
            flex-flow: row nowrap;
            justify-content: space-between;
            flex-grow: 1;
            margin: 0 10px;
            background: #1F2124;
            .unit {
                position: absolute;
                right: 35px;
            }
        }
    }
</style>

以下为引用number-box.vue组件的index.vue内容:

<template>
    <div class="main-container">
        <div class="" @click="onBackOff">
        </div>
        <number-box ref="numRange" :curVal="currentVal" :minVal="50" :maxVal=“1000” @getNumber="getNumber" />
    </div>
</template>
<script>
    export default {
        data() {
            return {
                currentVal: 30
            }
        },
        methods: {
            onBackOff() {    //重置子组件输入框中的内容
                this.$refs.numRange.resetCurVal(50);
            },
            getNumber(val) {
                console.log("This is value of input ", val);
            }
        }
    }
</script>

 

转载于:https://www.cnblogs.com/minozMin/p/9856279.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值