前言
在移動端或者小程序項目中,驗證碼輸入框、密碼輸入框也是很常見的,今天我們就來實現(xiàn)一個這樣的效果。
圖片展示
代碼實現(xiàn)
我這里是用uniapp實現(xiàn)的可兼容微信小程序。
大家如果需要微信小程序也可以參考此案例,實現(xiàn)思路都是一樣的。文章來源:http://www.zghlxwxcb.cn/news/detail-449964.html
<template>
<view class="index">
<view class="content">
<view class="phone-item">
<!-- 手機號輸入框 -->
<input type="number" class="phone-input" v-model="phone" placeholder="請輸入手機號">
<!-- 發(fā)送驗證碼按鈕 -->
<view class="get-code" @click="getCode" v-if="codeBtn.isCode || codeBtn.codeNumber == 0">{{codeBtn.codeName}}
</view>
<view class="get-code" v-else>{{codeBtn.codeNumber}}s</view>
</view>
<view class="input-list">
<!-- input輸入框 -->
<input class="input-item" v-if="focus" adjust-position="false" auto-blur="true" @blur="inputCodeBlur"
@input="inputCode" :focus="focus" v-model="code" @focus="inputFocus" type="number" oneTimeCode
maxlength="6" />
<!-- 驗證碼輸入框 -->
<view class="code-list" @click="focusClick">
<view class="code-item" v-for="(item,index) in 6" :key="index"
:style="(index == code.length && focus ? 'border-color:#3c9cff;':'')">{{code[index]}}</view>
</view>
</view>
</view>
</view>
</template>
<script>
export default {
data() {
return {
phone: '', // 手機號
timer: null, // 定時器
codeBtn: { // 按鈕狀態(tài)切換
codeName: '獲取驗證碼', // 狀態(tài)名稱
codeNumber: 2, // 倒計時秒數(shù)
isCode: true // 是否獲取驗證碼
},
code: '', // 驗證碼字段
focus: false, // input是否聚焦
}
},
watch: {
// 監(jiān)聽倒計時
'codeBtn.codeNumber': {
handler(val) {
// 這里監(jiān)聽用戶輸入完完整的驗證碼,去調(diào)接口驗證。
if (val == 0) {
this.codeBtn.codeName = '重新獲取'
clearInterval(this.timer)
}
}
}
},
methods: {
// 獲取驗證碼
getCode() {
this.codeBtn.isCode = false
this.codeBtn.codeNumber = 2
this.timer = setInterval(() => {
if (this.codeBtn.codeNumber == 0) {
clearInterval(this.timer)
return
}
this.codeBtn.codeNumber--
}, 1000)
},
// 點擊聚焦輸入框
focusClick() {
this.focus = true
},
// 輸入框失去焦點
inputCodeBlur(e) {
let value = e.detail.value
this.focus = false
},
// 輸入框聚焦時觸發(fā)(沒用到)
inputFocus(e, height) {
console.log(e)
},
// 輸入框內(nèi)容變化事件
inputCode(e) {
let value = e.detail.value
this.code = value
},
}
}
</script>
<style lang="scss" scoped>
.index {
padding: 30rpx;
.content {
padding: 20rpx;
.phone-item {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 30rpx;
.phone-input {
width: calc(100% - 240rpx);
padding: 16rpx;
border-bottom: 1rpx solid #eee;
}
.get-code {
text-align: center;
width: 170rpx;
font-size: 26rpx;
border-radius: 50rpx;
padding: 10rpx 0rpx;
background: #3c9cff;
color: #fff;
}
}
.input-list {
display: flex;
align-items: center;
.input-item {
width: 0rpx;
}
.code-list {
width: 100%;
display: flex;
align-items: center;
justify-content: space-between;
.code-item {
width: 80rpx;
height: 80rpx;
text-align: center;
line-height: 80rpx;
border: 1rpx solid #eee;
border-radius: 10rpx;
}
}
}
}
}
</style>
總結(jié)
這里遇到一個小小的坑
如果大家在微信小程序真機預(yù)覽的時候,輸入值時并未改變視圖,建議大家看一下自己的真機調(diào)試時的版本,改為 2.0 即可
具體可看這篇文章:微信小程序真機調(diào)試@input不生效文章來源地址http://www.zghlxwxcb.cn/news/detail-449964.html
到了這里,關(guān)于uniapp自定義輸入框,實現(xiàn)驗證碼輸入框、密碼輸入框、兼容微信小程序的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!