vue實現(xiàn)移動端多格輸入框
更新時間:2021年10月20日 14:39:00 作者:夢想家小阿新
這篇文章主要為大家詳細(xì)介紹了vue實現(xiàn)移動端多格輸入框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
近來公司提出需求,完成如下圖h5頁面操作。

網(wǎng)上沒什么輪子可以使用,就自己徒手?jǐn)]了一個。不多廢話,直接上代碼。
<div class="verify-tel">
<h1>短信驗證</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為短信驗證
digits:['','','','','',''], //input框位數(shù)控制,這里可以配置多少個“輸入框”
msg:'',
msgLength:0,
}
},
methods:{
//手機(jī)號碼驗證接口函數(shù)
verifyTels:async function () {
try{
}catch(e){
console.log(e)
}
},
//使input框獲得焦點
focus(){
this.$refs.pwd.focus();
},
},
beforeMount:function () {
//這里因為我的業(yè)務(wù)需求存在多個page,且digits由父組件傳輸過來,故我隱去,只剩下一句代碼。
this.page=1;
},
watch: {
msg(curVal){
//監(jiān)聽輸入的長度,如果輸入完,自動調(diào)用校驗接口函數(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ō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)文章
vue 和vue-touch 實現(xiàn)移動端左右導(dǎo)航效果(仿京東移動站導(dǎo)航)
這篇文章主要介紹了vue 和vue-touch 實現(xiàn)移動端左右導(dǎo)航效果(仿京東移動站導(dǎo)航),需要的朋友可以參考下2017-04-04
vue基于websocket實現(xiàn)智能聊天及吸附動畫效果
這篇文章主要介紹了vue基于websocket實現(xiàn)智能聊天及吸附動畫效果,主要功能是基于websocket實現(xiàn)聊天功能,封裝了一個socket.js文件,使用Jwchat插件實現(xiàn)類似QQ、微信電腦端的功能,本文通過實例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07
vue 本地環(huán)境跨域請求proxyTable的方法
今天小編就為大家分享一篇vue 本地環(huán)境跨域請求proxyTable的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09

