這篇文章主要介紹了vue怎么實(shí)現(xiàn)驗(yàn)證碼輸入框組件,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
創(chuàng)新互聯(lián)長(zhǎng)期為數(shù)千家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為西充企業(yè)提供專業(yè)的成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè),西充網(wǎng)站改版等技術(shù)服務(wù)。擁有10余年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。
Vue是一套用于構(gòu)建用戶界面的漸進(jìn)式JavaScript框架,Vue與其它大型框架的區(qū)別是,使用Vue可以自底向上逐層應(yīng)用,其核心庫(kù)只關(guān)注視圖層,方便與第三方庫(kù)和項(xiàng)目整合,且使用Vue可以采用單文件組件和Vue生態(tài)系統(tǒng)支持的庫(kù)開發(fā)復(fù)雜的單頁(yè)應(yīng)用。
先來(lái)看波完成效果圖
需求
輸入4位或6位短信驗(yàn)證碼,輸入完成后收起鍵盤
實(shí)現(xiàn)步驟
第一步
布局排版
<div class="security-code-wrap"> <label for="code"> <ul class="security-code-container"> <li class="field-wrap" v-for="(item, index) in number" :key="index"> <i class="char-field">{{value[index] || placeholder}}</i> </li> </ul> </label> <input ref="input" class="input-code" @keyup="handleInput($event)" v-model="value" id="code" name="code" type="tel" :maxlength="number" autocorrect="off" autocomplete="off" autocapitalize="off"> </div>
使用li元素來(lái)模擬輸入框的顯示,沒(méi)有別的目的,就只是為了語(yǔ)義化,當(dāng)然你也可以使用其他任意一個(gè)元素來(lái)模擬,比如div。
使用label標(biāo)簽的好處在于它可以跟input的click事件關(guān)聯(lián)上,一方面實(shí)現(xiàn)了語(yǔ)義化解決方案,另一方面也省去了我們通過(guò)js來(lái)喚起虛擬鍵盤。
隱藏輸入框
.input-code { position: absolute; left: -9999px; top: -99999px; width: 0; height: 0; opacity: 0; overflow: visible; z-index: -1; }
將真實(shí)的輸入框定位到屏幕可視區(qū)域以外的地方,虛擬鍵盤被喚起時(shí),就不會(huì)將頁(yè)面往上頂了。所以你的驗(yàn)證碼輸入組件一定要放在虛擬鍵盤遮擋不了的地方。
第二步
處理驗(yàn)證碼輸入
handleSubmit() { this.$emit('input', this.value) }, handleInput(e) { this.$refs.input.value = this.value if (this.value.length >= this.number) { this.hideKeyboard() } this.handleSubmit() }
輸入時(shí),給輸入框賦一次值,是為了解決android端上輸入框失焦后重新聚焦,輸入光標(biāo)會(huì)定在第一位的前面,經(jīng)過(guò)賦值再聚焦,光標(biāo)的位置就會(huì)顯示在最后一位后面。
第三步
完成輸入后關(guān)閉虛擬鍵盤
hideKeyboard() { // 輸入完成隱藏鍵盤 document.activeElement.blur() // ios隱藏鍵盤 this.$refs.input.blur() // android隱藏鍵盤 }
組件完整代碼
<!--四位驗(yàn)證碼輸入框組件--> <template> <div class="security-code-wrap"> <label for="code"> <ul class="security-code-container"> <li class="field-wrap" v-for="(item, index) in number" :key="index"> <i class="char-field">{{value[index] || placeholder}}</i> </li> </ul> </label> <input ref="input" class="input-code" @keyup="handleInput($event)" v-model="value" id="code" name="code" type="tel" :maxlength="number" autocorrect="off" autocomplete="off" autocapitalize="off"> </div> </template> <script> export default { name: 'SecurityCode', // component properties props: { number: { type: Number, default: 4 }, placeholder: { type: String, default: '-' } }, // variables data() { return { value: '' } }, methods: { hideKeyboard() { // 輸入完成隱藏鍵盤 document.activeElement.blur() // ios隱藏鍵盤 this.$refs.input.blur() // android隱藏鍵盤 }, handleSubmit() { this.$emit('input', this.value) }, handleInput(e) { this.$refs.input.value = this.value if (this.value.length >= this.number) { this.hideKeyboard() } this.handleSubmit() } } } </script> <style scoped lang="less"> .security-code-wrap { overflow: hidden; } .security-code-container { margin: 0; padding: 0; display: flex; justify-content: center; .field-wrap { list-style: none; display: block; width: 40px; height: 40px; line-height: 40px; font-size: 16px; background-color: #fff; margin: 2px; color: #000; .char-field { font-style: normal; } } } .input-code { position: absolute; left: -9999px; top: -99999px; width: 0; height: 0; opacity: 0; overflow: visible; z-index: -1; } </style>
組件使用代碼
<security-code v-model="authCode"></security-code>
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“vue怎么實(shí)現(xiàn)驗(yàn)證碼輸入框組件”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!
本文名稱:vue怎么實(shí)現(xiàn)驗(yàn)證碼輸入框組件
文章起源:http://www.chinadenli.net/article2/ggiioc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、面包屑導(dǎo)航、企業(yè)建站、外貿(mào)網(wǎng)站建設(shè)、移動(dòng)網(wǎng)站建設(shè)、建站公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)