vue實(shí)現(xiàn)移動(dòng)端多格輸入框
近來公司提出需求,完成如下圖h5頁面操作。

網(wǎng)上沒什么輪子可以使用,就自己徒手?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)容過于繁瑣,請自行省去修改)
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由父組件傳輸過來,故我隱去,只剩下一句代碼。
this.page=1;
},
watch: {
msg(curVal){
//監(jiān)聽輸入的長度,如果輸入完,自動(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)入頁面,自動(dòng)獲得焦點(diǎn),這里可優(yōu)化。
this.timer = setTimeout(()=>{this.$refs.pwd.focus()},500);
}
},
deep:true
}
},
beforeDestroy:function () {
clearTimeout(this.timer);
}
}
</script>
如果有問題,聯(lián)系本人修改。方便大家使用
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解Electron中如何使用SQLite存儲(chǔ)筆記
這篇文章主要為大家介紹了Electron中如何使用SQLite存儲(chǔ)筆記示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
vue項(xiàng)目兼容ie11的實(shí)現(xiàn)方法
本文主要介紹了vue項(xiàng)目兼容ie11的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
vue 和vue-touch 實(shí)現(xiàn)移動(dòng)端左右導(dǎo)航效果(仿京東移動(dòng)站導(dǎo)航)
這篇文章主要介紹了vue 和vue-touch 實(shí)現(xiàn)移動(dòng)端左右導(dǎo)航效果(仿京東移動(dòng)站導(dǎo)航),需要的朋友可以參考下2017-04-04
vue實(shí)現(xiàn)循環(huán)切換動(dòng)畫
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)循環(huán)切換動(dòng)畫,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10
vue基于websocket實(shí)現(xiàn)智能聊天及吸附動(dòng)畫效果
這篇文章主要介紹了vue基于websocket實(shí)現(xiàn)智能聊天及吸附動(dòng)畫效果,主要功能是基于websocket實(shí)現(xiàn)聊天功能,封裝了一個(gè)socket.js文件,使用Jwchat插件實(shí)現(xiàn)類似QQ、微信電腦端的功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07
vue 本地環(huán)境跨域請求proxyTable的方法
今天小編就為大家分享一篇vue 本地環(huán)境跨域請求proxyTable的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09

