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