vue實(shí)現(xiàn)移動(dòng)端多格輸入框
近來(lái)公司提出需求,完成如下圖h5頁(yè)面操作。
網(wǎng)上沒(méi)什么輪子可以使用,就自己徒手?jǐn)]了一個(gè)。不多廢話,直接上代碼。
<div class="verify-tel"> <h1>短信驗(yàn)證</h1> <h2>SMS Verification</h2> <div> <input ref="pwd" :maxlength="digits.length" v-model="msg" style="position: absolute;z-index: -1;opacity: 0"/> <ul class="pwd-wrap" @click="focus"> <li v-for="(item,key) in digits" :style="{'margin-right': (100-10*digits.length)/(digits.length-1)+'%','width':'10%'}" > <span v-if="msgLength > key">{{msg.substring(key,key+1)}}</span> </li> </ul> </div> </div>
css部分(樣式內(nèi)容過(guò)于繁瑣,請(qǐng)自行省去修改)
html,body{ width: 100%; height: 100%; background: #fbf9fe; } .verify-tel{ background-color: #f9f9f9; margin: 0 30px; >p{ color: red; font-weight: bold; margin-top: 40px; margin-bottom: 30px; } >h1{ font-weight: 400; margin-bottom: 0; } >h2{ margin-top: 0; font-weight: 400; font-size: 14px; color: #858585; } .input-box{ margin-top: 30px; >input{ width: 10%; border: none; border-bottom: 1px solid grey; background-color: #f9f9f9; text-align: center; margin-right: 18%; font-size: 35px; &:focus{ border-bottom: 1px solid deepskyblue; } &:last-of-type{ margin-right: 0 !important; } } } .btn-box{ >button{ height: 40px; border: 1px solid #4e8cee; color: #4e8cee; background-color: white; border-radius: 4px; width: 30%; &:last-of-type{ float: right; width: 65%; height: 40px; color: white; background-color: rgb(78,140,238); border-radius: 4px; } } } } .pwd-wrap{ padding-left: 0; width: 100%; height: 44px; padding-bottom: 1px; margin: 0 auto; display: flex; display: -webkit-box; display: -webkit-flex; cursor: pointer; border: none; background-color: #f9f9f9; } .pwd-wrap li{ list-style-type:none; text-align: center; line-height: 44px; -webkit-box-flex: 1; flex: 1; -webkit-flex: 1; border: none; border-bottom:1px solid black; background-color: #f9f9f9; &:last-of-type{ margin-right: 0 !important; } >span{ font-size: 20px; } } .pwd-wrap li:last-child{ border-right: 0; } .pwd-wrap li i{ height: 10px; width: 10px; border-radius:50% ; background: #000; display: inline-block; }
js部分代碼
<script> export default{ data(){ return { page:0, //1為短信驗(yàn)證 digits:['','','','','',''], //input框位數(shù)控制,這里可以配置多少個(gè)“輸入框” msg:'', msgLength:0, } }, methods:{ //手機(jī)號(hào)碼驗(yàn)證接口函數(shù) verifyTels:async function () { try{ }catch(e){ console.log(e) } }, //使input框獲得焦點(diǎn) focus(){ this.$refs.pwd.focus(); }, }, beforeMount:function () { //這里因?yàn)槲业臉I(yè)務(wù)需求存在多個(gè)page,且digits由父組件傳輸過(guò)來(lái),故我隱去,只剩下一句代碼。 this.page=1; }, watch: { msg(curVal){ //監(jiān)聽(tīng)輸入的長(zhǎng)度,如果輸入完,自動(dòng)調(diào)用校驗(yàn)接口函數(shù) if(curVal.trim().length===this.digits.length){ this.verifyTels(); } this.msgLength = curVal.length; }, 'page':{ handler:function (newValue, oldValue) { if(oldValue==0&&newValue==1){ //第一次進(jìn)入頁(yè)面,自動(dòng)獲得焦點(diǎn),這里可優(yōu)化。 this.timer = setTimeout(()=>{this.$refs.pwd.focus()},500); } }, deep:true } }, beforeDestroy:function () { clearTimeout(this.timer); } } </script>
如果有問(wèn)題,聯(lián)系本人修改。方便大家使用
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue項(xiàng)目webpack打包部署到服務(wù)器的實(shí)例詳解
這篇文章主要介紹了Vue項(xiàng)目webpack打包部署到服務(wù)器的實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-07-07Vue中靈活拖拽的前端神器VueDraggablePlus的用法詳解
這篇文章主要介紹了一款功能強(qiáng)大、靈活易用的前端組件VueDraggablePlus,作為前端工程師,我們經(jīng)常會(huì)遇到需要實(shí)現(xiàn)拖拽功能的場(chǎng)景,而VueDraggablePlus正是為了解決這一痛點(diǎn)而誕生的,讓我們一起來(lái)看看它的特點(diǎn)和用法吧2024-03-03vue+element實(shí)現(xiàn)批量刪除功能的示例
下面小編就為大家分享一篇vue+element實(shí)現(xiàn)批量刪除功能的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-02-02Vue3中按需引入ECharts詳細(xì)步驟(一看就會(huì))
新項(xiàng)目采用Vue3作為前端項(xiàng)目框架,避免不了要使用echarts,這篇文章主要給大家介紹了關(guān)于Vue3中按需引入ECharts的相關(guān)資料,需要的朋友可以參考下2023-09-09Vue項(xiàng)目History模式404問(wèn)題解決方法
本文主要解決Vue項(xiàng)目使用History模式發(fā)布到服務(wù)器Nginx上刷新頁(yè)面404問(wèn)題。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-10-10詳解Vue項(xiàng)目在其他電腦npm run dev運(yùn)行報(bào)錯(cuò)的解決方法
這篇文章主要介紹了詳解Vue項(xiàng)目在其他電腦npm run dev運(yùn)行報(bào)錯(cuò)的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10Vue實(shí)現(xiàn)關(guān)聯(lián)頁(yè)面多級(jí)跳轉(zhuǎn)(頁(yè)面下鉆)功能的完整實(shí)例
這篇文章主要給大家介紹了關(guān)于Vue實(shí)現(xiàn)關(guān)聯(lián)頁(yè)面多級(jí)跳轉(zhuǎn)(頁(yè)面下鉆)功能的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03