vue 实现6位验证码输入功能

本文将介绍如何使用Vue.js框架来实现一个六位数字验证码的输入功能,包括获取验证码后,用户逐个输入数字的过程。

实现获取验证码后,输入数字,依次填写到input框中.如图所示:
在这里插入图片描述
在这里插入图片描述

<div class="input-box code font-Toyota-Semibold">
    <div class="input-content d-flex" @keydown="keydown" @keyup="keyup" @paste="paste" @mousewheel="mousewheel" @input="inputEvent">
        <input class="code-text" max="9" min="0" maxlength="1" data-index="0" v-model.trim.number="input[0]" type="number" ref="firstinput" />
        <input class="code-text" max="9" min="0" maxlength="1" data-index="1" v-model.trim.number="input[1]" type="number" />
        <input class="code-text" max="9" min="0" maxlength="1" data-index="2" v-model.trim.number="input[2]" type="number" />
        <input class="code-text" max="9" min="0" maxlength="1" data-index="3" v-model.trim.number="input[3]" type="number" />
        <input class="code-text" max="9" min="0" maxlength="1" data-index="4" v-model.trim.number="input[4]" type="number" />
        <input class="code-text" max="9" min="0" maxlength="1" data-index="5" v-model.trim.number="input[5]" type="number" />
    </div>
</div>
data() {
    return {
        pasteResult: [],
    }
},
computed: {
    input() {
        return this.pasteResult.length === 6 ? this.pasteResult : ['', '', '', '', '', '']
    }
},
methods: {
    inputEvent(e) {
        var index = e.target.dataset.index * 1;
        var el = e.target;
        this.$set(this.input, index, el.value.slice(0, 1))
    },
    keydown(e) {
        var index = e.target.dataset.index * 1;
        var el = e.target;
        if (e.key === 'Backspace') {
             if (this.input[index].length > 0) {
                   this.$set(this.input, index, '')
             } else {
                   if (el.previousElementSibling) {
                        el.previousElementSibling.focus()
                        this.$set(this.input, index - 1, '')
                   }
             }
        } else if (e.key === 'Delete') {
              if (this.input[index].length > 0) {
                    this.$set(this.input, index, '')
              } else {
                   if (el.nextElementSibling) {
                       this.$set(this.input, index = 1, '')
                   }
              }
              if (el.nextElementSibling) {
                    el.nextElementSibling.focus()
              }
        } else if (e.key === 'Home') {
             el.parentElement.children[0] && el.parentElement.children[0].focus()
        } else if (e.key === 'End') {
             el.parentElement.children[this.input.length - 1] && el.parentElement.children[this.input.length - 1].focus()
        } else if (e.key === 'ArrowLeft') {
              if (el.previousElementSibling) {
                  el.previousElementSibling.focus()
               }
        } else if (e.key === 'ArrowRight') {
               if (el.nextElementSibling) {
                   el.nextElementSibling.focus()
               }
        } else if (e.key === 'ArrowUp') {
               if (this.input[index] * 1 < 9) {
                   this.$set(this.input, index, (this.input[index] * 1 + 1).toString());
               }
        } else if (e.key === 'ArrowDown') {
               if (this.input[index] * 1 > 0) {
                   this.$set(this.input, index, (this.input[index] * 1 - 1).toString());
               }
         }
    },
    keyup(e) {
         var index = e.target.dataset.index * 1;
         var el = e.target;
         if (/Digit|Numpad/i.test(e.code)) {
              this.$set(this.input, index, e.code.replace(/Digit|Numpad/i, ''));
              el.nextElementSibling && el.nextElementSibling.focus();
              if (index === 5) {
                   if (this.input.join('').length === 6) {
                        document.activeElement.blur();
                        this.$emit('complete', this.input);
                    }
              }
        } else {
              if (this.input[index] === '') {
                    this.$set(this.input, index, '');
              }
        }
    },
    mousewheel(e) {
        var index = e.target.dataset.index;
        if (e.wheelDelta > 0) {
             if (this.input[index] * 1 < 9) {
                  this.$set(this.input, index, (this.input[index] * 1 + 1).toString());
             }
        } else if (e.wheelDelta < 0) {
             if (this.input[index] * 1 > 0) {
                  this.$set(this.input, index, (this.input[index] * 1 - 1).toString());
             }
        } else if (e.key === 'Enter') {
             if (this.input.join('').length === 6) {
                  document.activeElement.blur();
                  this.$emit('complete', this.input);
             }
         }
     },
     paste(e) {
         e.clipboardData.items[0].getAsString(str => {
              if (str.toString().length === 6) {
                   this.pasteResult = str.split('');
                   document.activeElement.blur();
                   this.$emit('complete', this.input);
              }
          })
    } 
},
mounted() {
     this.$nextTick(() => {
          this.$refs.firstinput.focus()
     })
},
.code-text {
    width: 48px;
    height: 64px;
    background: #F6F7F7;
    border-radius: 15px;
    font-size: 24px;
    text-align: center;
    line-height: 64px;
}

.code-text:not(:first-child) {
    margin-left: 16px;
}

span {
    position: absolute;
    color: #EF5A40;
    font-size: 12px;
    top: 72px;
}

.input-box .input-content {
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.input-box .input-content input {
    color: inherit;
    font-family: inherit;
    border: 0;
    outline: 0;
}

.input-box .input-content input::-webkit-outer-spin-button,
.input-box .input-content input::-webkit-inner-spin-button {
    appearance: none;
    margin: 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值