实现获取验证码后,输入数字,依次填写到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;
}
本文将介绍如何使用Vue.js框架来实现一个六位数字验证码的输入功能,包括获取验证码后,用户逐个输入数字的过程。

1万+

被折叠的 条评论
为什么被折叠?



