利用vue實現(xiàn)密碼輸入框/驗證碼輸入框
更新時間:2023年08月30日 15:11:19 作者:前端61
這篇文章主要為大家詳細介紹了如何利用vue實現(xiàn)密碼輸入框或驗證碼輸入框的效果,文中的示例代碼講解詳細,感興趣的小伙伴可以參考一下
1.效果預(yù)覽


2.實現(xiàn)思路
制作6個小的正方形div
用一個input覆蓋在6個div上
給input設(shè)置透明(隱藏掉input)
3.源碼
html
<div class="input-box flexbox">
<div class="code-item" :class="codeValue.length == 0 && inputFocus ? 'code-item-active' : ''">{{codeValue[0]}}</div>
<div class="code-item" :class="codeValue.length == 1 && inputFocus ? 'code-item-active' : ''">{{codeValue[1]}}</div>
<div class="code-item" :class="codeValue.length == 2 && inputFocus ? 'code-item-active' : ''">{{codeValue[2]}}</div>
<div class="code-item" :class="codeValue.length == 3 && inputFocus ? 'code-item-active' : ''">{{codeValue[3]}}</div>
<div class="code-item" :class="codeValue.length == 4 && inputFocus ? 'code-item-active' : ''">{{codeValue[4]}}</div>
<div class="code-item" :class="codeValue.length >= 5 && inputFocus ? 'code-item-active' : ''">{{codeValue[5]}}</div>
<el-input class="input-code"
:value="codeValue"
:maxlength="6"
@blur="codeInputBlur"
@focus="codeInputFocus"
@input="codeInputChange">
</el-input>
</div>css
.input-box {
margin-top: 20px;
position: relative;
}
.input-code {
position: absolute;
}
.code-item {
width: 50px;
height: 50px;
text-align: center;
line-height: 50px;
border: 1px solid #f0f0f0;
margin-right: 10px;
}
.code-item-active {
border: 1px solid #F23026;
box-sizing: border-box;
}
// 隱藏input
.input-box {
.el-input__inner {
width: 362px;
height: 50px;
background-color: transparent;
border: none;
color: transparent;
}
}js
data() {
return {
codeValue: '',
inputFocus: false,
sendCodeFlag: false,
codeTime: 59,
};
},
methods: {
// 發(fā)送驗證碼
sendCode() {
this.codeTime = 59;
this.sendCodeFlag = true;
const timer = setInterval(() => {
this.codeTime -= 1;
if (this.codeTime <= 0) {
this.sendCodeFlag = false;
clearInterval(timer);
}
}, 1000);
},
// 驗證碼輸入框
codeInputChange(e) {
if (e) {
// 判斷輸入內(nèi)容是否為數(shù)字
if ((/^\+?[0-9][0-9]*$/).test(e)) {
this.codeValue = e;
}
} else {
this.codeValue = '';
}
},
// 驗證碼輸入框失去焦點
codeInputBlur() {
this.inputFocus = false;
},
// 驗證碼輸入框獲取到焦點
codeInputFocus() {
this.inputFocus = true;
},
},到此這篇關(guān)于利用vue實現(xiàn)密碼輸入框/驗證碼輸入框的文章就介紹到這了,更多相關(guān)vue輸入框內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于Vue3?+?Element?Plus實現(xiàn)的日期選擇器
本文介紹了使用Vue3和ElementPlus實現(xiàn)日期選擇器的方法,包括禁用邏輯聯(lián)動、設(shè)定結(jié)束時間不能選今天、時間格式化等功能,通過示例代碼展示了兩種常用場景的實現(xiàn)方式,適用于直接復(fù)制使用,需要的朋友可以參考下2026-03-03
vue指令只能輸入正數(shù)并且只能輸入一個小數(shù)點的方法
這篇文章主要介紹了vue指令只能輸入正數(shù)并且只能輸入一個小數(shù)點的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06
elementUI實現(xiàn)下拉選項加多選框的示例代碼
因產(chǎn)品需求和UI樣式調(diào)整,本文主要實現(xiàn)elementUI下拉選項加多選框的示例代碼,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-10-10

