vue3实现6位数验证码自动聚焦编辑效果

如图所示

不多bb直接上代码 

页面代码

<template>
    <div class="input-box">
        <div class="code-item" :class="codeValue.length == 0 && inputFocus ? 'code-item-active' : ''">{{ codeValue[0] }}
        </div>
        <div class="code-item" :class="codeValue.length == 1 && inputFocus ? 'code-item-active' : ''">{{ codeValue[1] }}
        </div>
        <div class="code-item" :class="codeValue.length == 2 && inputFocus ? 'code-item-active' : ''">{{ codeValue[2] }}
        </div>
        <div class="code-item" :class="codeValue.length == 3 && inputFocus ? 'code-item-active' : ''">{{ codeValue[3] }}
        </div>
        <div class="code-item" :class="codeValue.length == 4 && inputFocus ? 'code-item-active' : ''">{{ codeValue[4] }}
        </div>
        <div class="code-item" :class="codeValue.length >= 5 && inputFocus ? 'code-item-active' : ''">{{ codeValue[5] }}
        </div>
        <input class="input-code" v-model="codeValue" :maxlength="6" type="tel" @blur="codeInputBlur"
            @focus="codeInputFocus" @input="codeInputChange" />
    </div>
</template>

ts代码

<script lang='ts' setup>
import { onMounted, ref, Ref } from "vue";
let time: Ref<number> = ref(0)
let codeValue: Ref<string> = ref('')
let inputFocus: Ref<boolean> = ref(false)
onMounted(() => {
    getVerifyCode()
})
// 获取验证码
const getVerifyCode = () => {
    time.value = 120;
    const timer = setInterval(() => {
        time.value--;
        if (time.value <= 0) {
            time.value = 0;
            clearInterval(timer);
        }
    }, 1000);
};
// 验证码输入框input
const codeInputChange = () => {
    if (codeValue.value && codeValue.value.length >= 6) {
        // do soming...
    }
}
// 验证码输入框失去焦点
const codeInputBlur = () => {
    inputFocus.value = false;
}
// 验证码输入框获取到焦点
const codeInputFocus = () => {
    inputFocus.value = true;
}
</script>

css代码

<style lang="scss" scoped>
.input-box {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: space-around;
    margin-top: 80px;
    position: relative;
    padding: 0px 15px;
    box-sizing: border-box;

    .input-code {
        width: 100%;
        height: 50px;
        position: absolute;
        left: 0px;
        top: 0px;
        box-sizing: border-box;
        color: transparent;
        background-color: transparent;
        opacity: 0;
    }

    .code-item {
        width: 40px;
        height: 50px;
        text-align: center;
        line-height: 50px;
        border: 1px solid#D8D8D8;
        margin-right: 10px;
        color: #444;
        font-size: 34px;
    }

    .code-item-active {
        border-bottom: 1px solid#F23026;
    }
}
</style>

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3和TypeScript中实现邮箱验证码功能,可以按照以下步骤进行: 1. 安装依赖:首先,确保你已经安装了Vue3和TypeScript的开发环境。然后,使用npm或yarn安装相关依赖: ``` npm install axios ``` 2. 创建组件:创建一个名为`EmailVerification`的组件,用于发送邮箱验证码和验证验证码的功能。 3. 在组件中引入axios:在`EmailVerification`组件中,引入axios库,用于发送HTTP请求。 4. 发送验证码请求:在`EmailVerification`组件中,创建一个方法,用于发送验证码请求。可以使用axios库发送POST请求到后端API,请求中包含邮箱地址作为参数。 5. 后端验证:后端接收到邮箱地址后,生成验证码并发送到该邮箱。后端需要实现生成验证码的逻辑,并将验证码与邮箱地址进行关联。 6. 验证码输入和验证:在`EmailVerification`组件中,创建一个数据属性来存储用户输入的验证码。同时,创建一个方法来验证用户输入的验证码是否正确。可以通过axios库发送POST请求到后端API,请求中包含用户输入的验证码和邮箱地址作为参数。 7. 组件使用:在需要使用邮箱验证码的地方,引入`EmailVerification`组件,并使用它。 下面是一个简单的示例代码: ```vue <template> <div> <input v-model="email" type="email" placeholder="请输入邮箱地址" /> <button @click="sendVerificationCode">发送验证码</button> <input v-model="verificationCode" type="text" placeholder="请输入验证码" /> <button @click="verifyCode">验证</button> </div> </template> <script lang="ts"> import { defineComponent } from 'vue'; import axios from 'axios'; export default defineComponent({ data() { return { email: '', verificationCode: '', }; }, methods: { sendVerificationCode() { axios.post('/api/sendVerificationCode', { email: this.email }) .then(response => { // 验证码发送成功 }) .catch(error => { // 验证码发送失败 }); }, verifyCode() { axios.post('/api/verifyCode', { email: this.email, code: this.verificationCode }) .then(response => { // 验证成功 }) .catch(error => { // 验证失败 }); }, }, }); </script> ``` 请注意,上述代码中的`/api/sendVerificationCode`和`/api/verifyCode`是示例的后端API接口地址,你需要根据实际情况进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值