JS如何實(shí)現(xiàn)手機(jī)端輸入驗(yàn)證碼效果
之前在“掘金”上看到這樣一個(gè)demo 我覺得很有意思,于是今天把它搬下來,記在自己的“小本本”里
也許會(huì)對以后的項(xiàng)目有點(diǎn)用,若要自己去實(shí)現(xiàn)這樣一個(gè)案例也能實(shí)現(xiàn),但是可能沒有那么“妙”。
想法:
1、使用label標(biāo)簽做顯示驗(yàn)證碼的框,
2、然后每個(gè)label for屬性指向同一個(gè) id 為vcode 的input,
3、為了能夠觸發(fā)input焦點(diǎn), 將input 改透明度樣式隱藏,
4、這樣就實(shí)現(xiàn)了 點(diǎn)擊label觸發(fā) input焦點(diǎn),調(diào)用鍵盤。
運(yùn)行效果:

示例代碼:
結(jié)構(gòu)部分html:
<div id="app" class="app">
<h2 class="heading-2">驗(yàn)證碼:</h2>
<div class="v-code">
<input
ref="vcode"
id="vcode"
type="tel"
maxlength="6"
v-model="code"
@focus="focused = true"
@blur="focused = false"
:disabled="telDisabled">
<label
for="vcode"
class="line"
v-for="item,index in codeLength"
:class="{'animated': focused && cursorIndex === index}"
v-text="codeArr[index]"
>
</label>
</div>
</div>
css部分:
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
background-color: #ffffff;
font-family: -apple-system, PingFang SC, Hiragino Sans GB, Helvetica Neue, Arial;
-webkit-tap-highlight-color: transparent;
}
.app {
padding-left: 20px;
padding-right: 20px;
padding-top: 60px;
max-width: 320px;
margin-left: auto;
margin-right: auto;
}
.heading-2 {
color: #333333;
}
.v-code {
margin-top: 20px;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-pack: justify;
-ms-flex-pack: justify;
justify-content: space-between;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
position: relative;
width: 280px;
margin-left: auto;
margin-right: auto;
}
.v-code input {
position: absolute;
top: 200%;
opacity:0;
}
.v-code .line {
position: relative;
width: 40px;
height: 32px;
line-height: 32px;
text-align: center;
font-size: 28px;
}
.v-code .line::after {
display: block;
position: absolute;
content: '';
left: 0;
width: 100%;
bottom: 0;
height: 1px;
background-color: #aaaaaa;
transform: scaleY(.5);
transform-origin: 0 100%;
}
.v-code .line.animated::before {
display: block;
position: absolute;
left: 50%;
top: 20%;
width: 1px;
height: 60%;
content: '';
background-color: #333333;
animation-name: coruscate;
animation-duration: 1s;
animation-iteration-count: infinite;
animation-fill-mode: both;
}
@keyframes coruscate {
0% {
opacity: 0
}
25% {
opacity: 0
}
50% {
opacity: 1
}
75% {
opacity: 1
}
to {
opacity: 0
}
}
</style>
Javascript部分
1、通過CDN引入vue.js
<script src="https://cdn.bootcss.com/vue/2.5.16/vue.min.js"></script>
2、代碼
var app = new Vue({
el: '#app',
data: {
code: '',
codeLength: 6,
telDisabled: false,
focused: false
},
computed: {
codeArr() {
return this.code.split('')
},
cursorIndex() {
return this.code.length
}
},
watch: {
code(newVal) {
this.code = newVal.replace(/[^\d]/g,'')
if (newVal.length > 5) {
// this.telDisabled = true
this.$refs.vcode.blur()
setTimeout(() => {
alert(`vcode: ${this.code}`)
}, 500)
}
}
},
})
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- JavaScript生成隨機(jī)驗(yàn)證碼代碼實(shí)例
- JS實(shí)現(xiàn)點(diǎn)擊發(fā)送驗(yàn)證碼 xx秒后重新發(fā)送功能
- php/JS實(shí)現(xiàn)的生成隨機(jī)密碼(驗(yàn)證碼)功能示例
- js實(shí)現(xiàn)隨機(jī)8位驗(yàn)證碼
- JS 驗(yàn)證碼功能的三種實(shí)現(xiàn)方式
- js+canvas實(shí)現(xiàn)滑動(dòng)拼圖驗(yàn)證碼功能
- js實(shí)現(xiàn)登錄注冊框手機(jī)號(hào)和驗(yàn)證碼校驗(yàn)(前端部分)
- JS驗(yàn)證碼實(shí)現(xiàn)代碼
相關(guān)文章
利用Ext Js生成動(dòng)態(tài)樹實(shí)例代碼
今天在公司幫同事寫了個(gè)用Ext Js生成動(dòng)態(tài)樹的Demo,在這里分享一下,也好供以后自己查閱。2008-09-09
JavaScript自定義超時(shí)API代碼實(shí)例
這篇文章主要介紹了JavaScript自定義超時(shí)API代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
JavaScript??际謱戭}之柯里化與數(shù)組扁平化的實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了JavaScript??际謱戭}中柯里化與數(shù)組扁平化、數(shù)組去重的實(shí)現(xiàn),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-12-12
js 關(guān)于=+與+=日期函數(shù)使用說明(賦值運(yùn)算符)
js 關(guān)于=+與+=日期函數(shù)使用說明(賦值運(yùn)算符),可以看下,就是一些運(yùn)算符的使用,看哪個(gè)更適合你。2011-11-11

