vue 实现数字验证码功能

news/2024/10/23 9:32:59/

需求:写了一个 手机发送验证码后 输入固定验证码的功能

 

 封装成一个组件,如下:

<template><div class="conts"><div class="box"><div class="code_list"><div :class="[ 'code_item', hideIndex == 0 ? 'code_item-focus' : '', value[0] ? 'shows_shaw' : '', ]">{{ value[0] }}</div><div :class="[ 'code_item', hideIndex == 1 ? 'code_item-focus' : '', value[1] ? 'shows_shaw' : '', ]">{{ value[1] }}</div><div :class="[ 'code_item', hideIndex == 2 ? 'code_item-focus' : '', value[2] ? 'shows_shaw' : '', ]">{{ value[2] }}</div><div :class="[ 'code_item', hideIndex == 3 ? 'code_item-focus' : '', value[3] ? 'shows_shaw' : '', ]">{{ value[3] }}</div></div><input v-model="value" class="field-input" type="number" maxlength="4" @input="input()" @focus="focus()" @blur="blur()" /></div></div>
</template><script>
export default {data() {return {value: '',index: 0,hideIndex: null,show_box: false,}},methods: {input() {const v = this.value.replace(/[^\d]/g, '');this.value = v;this.index = v.length;this.hideIndex = v.length;this.$emit('getPassword', v);},// 获取焦点focus() {this.hideIndex = this.index;this.show_box = true;},// 失去焦点blur() {this.hideIndex = null;this.show_box = false;},},
}
</script><style lang="less">
.conts {width: 100%;height: 100%;display: flex;align-items: center;justify-content: center;text-align: center;.box {position: relative;width: 85%;overflow: hidden;.code_list {display: flex;justify-content: space-between;border: 1px solid transparent;padding: 5px;border-radius: 3px;}}.field-input {box-sizing: border-box;position: absolute;top: 0;right: 0;width: 100%;height: 59px;padding: 0;border: none;outline: none;opacity: 0;background: transparent;}
}
.shows_shaw {border: 1px solid #0187fb !important;
}
.code_item {box-sizing: border-box;width: 59px;height: 59px;line-height: 59px;font-size: 24px;text-align: center;font-weight: 400;background-color: #f2f5f4;border: 1px solid transparent;border-radius: 4px;margin-right: 7px;&:last-child {margin-right: 0;}
}
.code_item-focus {border-color: #0187fb;&::before {content: "";display: block;width: 2px;height: 24px;margin: 17px auto;background: #0187fb;animation: blink 1s steps(1) infinite;}
}
@keyframes blink {50% {opacity: 0;}
}
</style>

引用

<room-password @getPassword="getPassword"></room-password>/引入
import roomPassword from '@/components/roomPassword'//方法getPassword(val) {console.log('val', val);},


http://www.ppmy.cn/news/1130754.html

相关文章

【Flink】

事件驱动型应用 核心目标&#xff1a;数据流上的有状态计算 Apache Flink是一个框架和分布式处理引擎&#xff0c;用于对无界或有界数据流进行有状态计算。 运行逻辑 状态 把流处理需要的额外数据保存成一个“状态”,然后针对这条数据进行处理,并且更新状态。这就是所谓的“…

孤举者难起,众行者易趋,openGauss 5.1.0版本正式发布!

&#x1f4e2;&#x1f4e2;&#x1f4e2;&#x1f4e3;&#x1f4e3;&#x1f4e3; 哈喽&#xff01;大家好&#xff0c;我是【IT邦德】&#xff0c;江湖人称jeames007&#xff0c;10余年DBA及大数据工作经验 一位上进心十足的【大数据领域博主】&#xff01;&#x1f61c;&am…

基于51单片机数字电压表仿真设计_数码管显示(程序+仿真+原理图+PCB+报告+讲解视频)

基于51单片机数字电压表仿真设计_数码管显示&#xff08;程序仿真原理图PCB报告讲解视频&#xff09; 原理图&#xff1a;Altium Designer 仿真版本&#xff1a;proteus 7.8及以上 程序编译器&#xff1a;keil 4/keil 5 编程语言&#xff1a;C语言 设计编号&#xff1a;S0…

java中发射实现的三种方式

三种反射实现方式 通过 new 对象实现反射机制&#xff1b;通过路径实现反射机制&#xff1b;通过类名实现反射机制。 对象.getClass() User user new User(); Class userClass1 user.getClass(); System.out.println(userClass1.getName());通过所在路径 Class userClass…

ESP32IDF — 硬件I2C使用教程

前言 &#xff08;1&#xff09;最近刚做完ESP32的一个模块的驱动移植&#xff0c;使用到了I2C。感觉ESP32的硬件I2C还是挺容易使用的。 &#xff08;2&#xff09;本文将只会介绍ESP32的硬件I2C使用&#xff0c;如果想知道软件I2C使用&#xff0c;可看其他的任意一款芯片软件I…

华为云云耀云服务器L实例评测|华为云云耀云服务器L实例CentOS的存储和备份策略

1 华为云云耀云服务器L实例介绍 华为云云耀云服务器L实例是华为云计算服务中的一种虚拟云服务器&#xff0c;它提供了强大的计算资源&#xff0c;可以在云端运行各种应用程序和服务。 华为云服务器提供了多种实例类型&#xff0c;包括通用型、计算优化型、内存优化型等&#…

【前端】HTML5 Audio 预加载 按照队列顺序播放音频, 可以陆续往队列中加内容

【前端】Audio 按照队列顺序播放音频, 可以陆续往队列中加内容 var 音频库 {} var 当前音频集合 [] /*** 将文本添加到队列中* 持续去播放* 播放过的音频会自动从队列中删除* * 已规划* 要保障同时进行加载的数据不能超过5个(线程池 5)* * param 文本*/播放音频队列(文本){i…

Ubuntu Kafka开机自启动服务

1、创建service文件 在/lib/systemd/system目录下创建kafka.service文件 [Unit] DescriptionApache Kafka Server Documentationhttp://kafka.apache.org/documentation.html Requireszookeeper.service[Service] Typesimple Environment"JAVA_HOME/usr/local/programs/j…