vue3實(shí)現(xiàn)密碼輸入框效果的示例代碼
參考:vue下的密碼輸入框
注意:這是個(gè)半成品,有些問(wèn)題(input輸入框加了文字間距l(xiāng)etter-spaceing,會(huì)導(dǎo)致輸入到第6位的時(shí)候會(huì)往后竄出來(lái)一個(gè)空白框、光標(biāo)位置頁(yè)會(huì)在數(shù)字前面),建議不采用下面這種方式,用另外的(畫(huà)六個(gè)input框更方便)
1.效果預(yù)覽

2.實(shí)現(xiàn)思路
制作6個(gè)小的正方形div
用一個(gè)input覆蓋在6個(gè)div上
給input設(shè)置透明(透明掉input)
3.源碼
html
<div class="footerTextStyle">請(qǐng)輸入6位數(shù)支付密碼</div>
<div class="input-box">
<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 code-item-last" :class="codeValue?.length >= 5 && inputFocus ? 'code-item-active' : ''">{{ codeValue[5]
}}
</div>
<a-input class="input-code" v-model:value="codeValue" type="" @blur="codeInputBlur"
@focus="codeInputFocus" @input="codeInputChange" maxlength="6" />
</div>js
// 輸入的6位密碼
const codeValue = ref([])
const inputFocus = ref(false)
// 密碼輸入框
const codeInputChange = (e : any) => {
console.log('喵喵喵:', e.data);
if (e) {
// // 判斷輸入內(nèi)容是否為數(shù)字
// if ((/^\+?[0-9][0-9]*$/).test(e)) {
// codeValue.value = e.data
// }
Array.from(codeValue.value).push(e.data)
} else {
codeValue.value = []
}
console.log('咩咩:', codeValue.value);
}
// 密碼輸入框失去焦點(diǎn)
const codeInputBlur = () => {
inputFocus.value = false
}
// 密碼輸入框獲取到焦點(diǎn)
const codeInputFocus = () => {
inputFocus.value = true
}css
<style lang="scss" scoped>
// 密碼輸入框
.input-box {
position: relative;
margin: 4px 0px 20px 0px;
display: flex;
width: 290px;
height: 48px;
background-color: transparent;
border: none;
color: transparent;
}
// 透明input
.input-code {
position: absolute;
width: 290px;
height: 48px;
background-color: transparent;
border: none;
letter-spacing: 40px;
padding: 0px 20px;
}
.code-item {
width: 48px;
height: 48px;
text-align: center;
line-height: 48px;
border: 1px solid rgba(51,51,51,0.20);
border-right: none;
border-radius: 5px;
}
.code-item-last{
border-right: 1px solid rgba(51,51,51,0.20);
}
.code-item-active {
border: 1px solid #F23026;
box-sizing: border-box;
}
</style>到此這篇關(guān)于vue3實(shí)現(xiàn)密碼輸入框效果的示例代碼的文章就介紹到這了,更多相關(guān)vue3密碼輸入框內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue封裝全局彈窗警告組件this.$message.success問(wèn)題
這篇文章主要介紹了vue封裝全局彈窗警告組件this.$message.success問(wèn)題,具有很的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
vue-cli 2.*中導(dǎo)入公共less文件的方法步驟
這篇文章主要介紹了vue-cli 2.*中導(dǎo)入公共less文件的方法步驟,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-11-11
vue自定義權(quán)限標(biāo)簽詳細(xì)代碼示例
這篇文章主要給大家介紹了關(guān)于vue自定義權(quán)限標(biāo)簽的相關(guān)資料,在Vue中你可以通過(guò)創(chuàng)建自定義組件來(lái)實(shí)現(xiàn)自定義標(biāo)簽組件,文中給出了詳細(xì)的代碼示例,需要的朋友可以參考下2023-09-09
Vue頁(yè)面布局與路由映射實(shí)戰(zhàn)教程之RouterView嵌套及動(dòng)態(tài)組件生成詳解
文章介紹了布局組件在Vue應(yīng)用中的設(shè)計(jì)和使用,包括布局組件的核心作用、多層RouterView的嵌套布局、靜態(tài)組件與動(dòng)態(tài)組件的區(qū)別,以及動(dòng)態(tài)路由的優(yōu)勢(shì),通過(guò)這些機(jī)制,可以實(shí)現(xiàn)界面的一致性、靈活的權(quán)限控制和菜單擴(kuò)展,感興趣的朋友跟隨小編一起看看吧2026-01-01
vant 解決tab切換插件標(biāo)題樣式自定義的問(wèn)題
這篇文章主要介紹了vant 解決tab切換插件標(biāo)題樣式自定義的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11
vue backtop組件的實(shí)現(xiàn)完整代碼
這篇文章主要介紹了vue backtop組件的實(shí)現(xiàn)完整代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04
vuex中this.$store.commit和this.$store.dispatch的基本用法實(shí)例
在vue的項(xiàng)目里常常會(huì)遇到父子組件間需要進(jìn)行數(shù)據(jù)傳遞的情況,下面這篇文章主要給大家介紹了關(guān)于vuex中this.$store.commit和this.$store.dispatch的基本用法的相關(guān)資料,需要的朋友可以參考下2023-01-01

