vue3實(shí)現(xiàn)密碼輸入框效果的示例代碼
參考:vue下的密碼輸入框
注意:這是個半成品,有些問題(input輸入框加了文字間距l(xiāng)etter-spaceing,會導(dǎo)致輸入到第6位的時候會往后竄出來一個空白框、光標(biāo)位置頁會在數(shù)字前面),建議不采用下面這種方式,用另外的(畫六個input框更方便)
1.效果預(yù)覽
2.實(shí)現(xiàn)思路
制作6個小的正方形div
用一個input覆蓋在6個div上
給input設(shè)置透明(透明掉input)
3.源碼
html
<div class="footerTextStyle">請輸入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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue3采用xlsx庫實(shí)現(xiàn)本地上傳excel文件功能
這篇文章主要為大家詳細(xì)介紹了vue3如何采用xlsx庫實(shí)現(xiàn)本地上傳excel文件功能,并且前端解析為Json數(shù)據(jù),感興趣的小伙伴可以了解一下2025-02-02Vue-CLI3.x 自動部署項(xiàng)目至服務(wù)器的方法步驟
本教程講解的是 Vue-CLI 3.x 腳手架搭建的vue項(xiàng)目, 利用scp2自動化部署到靜態(tài)文件服務(wù)器 Nginx,感興趣的可以了解一下2021-11-11vue組件講解(is屬性的用法)模板標(biāo)簽替換操作
這篇文章主要介紹了vue組件講解(is屬性的用法)模板標(biāo)簽替換操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09VUE3基于vite封裝條形碼和二維碼組件的詳細(xì)過程
基礎(chǔ)組件開發(fā)是項(xiàng)目業(yè)務(wù)開發(fā)的基石, 本文主要介紹了通過vue3的vite腳手架快速搭建項(xiàng)目, 開發(fā)條形碼和二維碼組件的過程,感興趣的朋友跟隨小編一起看看吧2023-08-08vue如何在項(xiàng)目中調(diào)用騰訊云的滑動驗(yàn)證碼
這篇文章主要介紹了vue如何在項(xiàng)目中調(diào)用騰訊云的滑動驗(yàn)證碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07