欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

uniapp中如何基于vue3實(shí)現(xiàn)輸入驗(yàn)證碼功能

 更新時(shí)間:2024年09月12日 10:29:01   作者:deku-yzh  
本文介紹了如何使用uniapp和vue3創(chuàng)建一個(gè)手機(jī)驗(yàn)證碼輸入框組件,通過封裝VerificationCodeInput.vue組件,并在父組件中引入,可以實(shí)現(xiàn)驗(yàn)證碼輸入功能,適合需要增加手機(jī)驗(yàn)證碼驗(yàn)證功能的開發(fā)者參考使用

實(shí)現(xiàn)效果

描述

使用uniapp和vue3實(shí)現(xiàn)了手機(jī)獲取驗(yàn)證碼后,輸入驗(yàn)證碼的輸入框功能

具體實(shí)現(xiàn)代碼

下述代碼為實(shí)現(xiàn)驗(yàn)證碼輸入框封裝的組件VerificationCodeInput.vue

<template>
  <view class="container">
    <view class="input-container">
      <view v-for="index in 4" :key="index" class="verify-input">
        <input
          type="number"
          class="input-field"
          :ref="`input${index - 1}`"
          :maxlength="1"
          :focus="focusIndex === index - 1"
          @input="handleInput(index - 1, $event)"
          @focus="handleFocus(index - 1)"
        />
      </view>
    </view>
  </view>
</template>
<script lang="ts" setup>
import { ref, onMounted, nextTick } from 'vue'
// 焦點(diǎn)索引
const focusIndex = ref(0)
// 輸入值數(shù)組
const values = ref<string[]>(['', '', '', ''])
// 輸入框ref數(shù)組
const inputRefs = ref<(HTMLInputElement | null)[]>([])
/**
 * 處理每個(gè)輸入值函數(shù)
 * @param index - 序號(hào).
 * @param event - 輸入事件對(duì)象.
 */
const handleInput = (index: number, event: Event) => {
  // 獲取輸入框的值
  const input = event.target as HTMLInputElement
  const value = input.value
  if (value) {
    // 更新輸入值數(shù)組
    values.value[index] = value
    // 判斷是否還有下一個(gè)輸入框,如果有則聚焦
    if (index < 3) {
      nextTick(() => {
        focusIndex.value = index + 1
        const nextInput = inputRefs.value[index + 1]
        nextInput?.focus()
      })
    }
    // 判斷是否所有輸入框都已經(jīng)有值,如果有則觸發(fā)完成事件
    if (values.value.every((v) => v.length > 0)) {
      handleComplete()
    }
  } else {
    // 如果輸入值為空,則聚焦前一個(gè)輸入框
    if (index > 0) {
      focusIndex.value = index - 1
      nextTick(() => {
        const prevInput = inputRefs.value[index - 1]
        prevInput?.focus()
      })
    }
  }
}
// 處理焦點(diǎn)事件
const handleFocus = (index: number) => {
  focusIndex.value = index
}
// 處理完成事件
const handleComplete = () => {
  const code = values.value.join('')
  console.log('驗(yàn)證碼輸入完成:', code)
}
onMounted(() => {
  // 初始化焦點(diǎn)
  nextTick(() => {
    const firstInput = inputRefs.value[0]
    firstInput?.focus()
  })
})
</script>
<style>
.input-container {
  display: flex;
  justify-content: space-between;
}
.verify-input {
  width: 60px;
  height: 60px;
  display: flex;
  justify-content: center;
  align-items: center;
}
.input-field {
  width: 100%;
  height: 100%;
  text-align: center;
  font-size: 24px;
  border: none;
  border-bottom: 2px solid #ccc;
  outline: none;
}
</style>

最后在父組件中引入VerificationCodeInput.vue即可實(shí)現(xiàn)上圖效果

到此這篇關(guān)于uniapp中基于vue3實(shí)現(xiàn)輸入驗(yàn)證碼功能的文章就介紹到這了,更多相關(guān)vue3輸入驗(yàn)證碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Vue中動(dòng)畫與過渡的使用教程

    Vue中動(dòng)畫與過渡的使用教程

    最近在寫vue的一個(gè)項(xiàng)目要實(shí)現(xiàn)過渡的效果,雖然vue動(dòng)畫不是強(qiáng)項(xiàng),庫也多,但是基本的坑還是得踩扎實(shí),下面這篇文章主要給大家介紹了關(guān)于Vue中實(shí)現(xiàn)過渡動(dòng)畫效果的相關(guān)資料,需要的朋友可以參考下
    2023-01-01
  • vue+Element-ui前端實(shí)現(xiàn)分頁效果

    vue+Element-ui前端實(shí)現(xiàn)分頁效果

    這篇文章主要為大家詳細(xì)介紹了vue+Element-ui前端實(shí)現(xiàn)分頁效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-11-11
  • vue中將網(wǎng)頁打印成pdf實(shí)例代碼

    vue中將網(wǎng)頁打印成pdf實(shí)例代碼

    本篇文章主要介紹了vue中將網(wǎng)頁打印成pdf實(shí)例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-06-06
  • vue中$set的使用(結(jié)合在實(shí)際應(yīng)用中遇到的坑)

    vue中$set的使用(結(jié)合在實(shí)際應(yīng)用中遇到的坑)

    這篇文章主要介紹了vue中$set的使用(結(jié)合在實(shí)際應(yīng)用中遇到的坑),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-07-07
  • Vue使用openlayers實(shí)現(xiàn)繪制圓形和多邊形

    Vue使用openlayers實(shí)現(xiàn)繪制圓形和多邊形

    這篇文章主要為大家詳細(xì)介紹了Vue如何使用openlayers實(shí)現(xiàn)繪制圓形和多邊形,文中的示例代碼講解詳細(xì),感興趣的小伙伴快跟隨小編一起動(dòng)手嘗試一下
    2022-06-06
  • vue引入Excel表格插件的方法

    vue引入Excel表格插件的方法

    這篇文章主要為大家詳細(xì)介紹了vue引入Excel表格插件的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-04-04
  • 又一款MVVM組件 構(gòu)建自己的Vue組件(2)

    又一款MVVM組件 構(gòu)建自己的Vue組件(2)

    這篇文章主要為大家分享了一款MVVM組件,構(gòu)建自己的Vue組件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • Vue?中?Promise?的then方法異步使用及async/await?異步使用總結(jié)

    Vue?中?Promise?的then方法異步使用及async/await?異步使用總結(jié)

    then?方法是?Promise?中?處理的是異步調(diào)用,異步調(diào)用是非阻塞式的,在調(diào)用的時(shí)候并不知道它什么時(shí)候結(jié)束,也就不會(huì)等到他返回一個(gè)有效數(shù)據(jù)之后再進(jìn)行下一步處理,這篇文章主要介紹了Vue?中?Promise?的then方法異步使用及async/await?異步使用總結(jié),需要的朋友可以參考下
    2023-01-01
  • vue+element?DatePicker實(shí)現(xiàn)日期選擇器封裝

    vue+element?DatePicker實(shí)現(xiàn)日期選擇器封裝

    Vue?Element?DatePicker是一款基于Vue.js的日期選擇控件,它提供了豐富的日期選擇功能,支持日期范圍選擇、日期格式化、自定義日期格式、快捷選擇等功能,極大地提高了用戶的體驗(yàn),是開發(fā)者必備的日期選擇控件。
    2023-02-02
  • vue滑動(dòng)解鎖組件使用方法詳解

    vue滑動(dòng)解鎖組件使用方法詳解

    這篇文章主要為大家詳細(xì)介紹了vue滑動(dòng)解鎖組件的使用方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03

最新評(píng)論