当前位置: 首页 > news >正文

【uniapp】短信验证码输入框

需求是短信验证码需要格子输入框 如图

在这里插入图片描述

网上找了一个案例改吧改吧 直接上代码

结构

在这里插入图片描述


<template><view class="verify-code"><!-- 输入框 --><input id="input" :value="code" class="input" :focus="isFocus" :type="inputType" :maxlength="itemSize"@input="onInput" @focus="inputFocus" @blur="inputBlur" /><!-- 光标 --><view id="cursor" v-if="cursorVisible && type !== 'middle'" class="cursor":style="{ left: codeCursorLeft[code.length] + 'px', height: cursorHeight + 'px', backgroundColor: cursorColor }"></view><!-- 输入框 ---><view id="input-ground" class="input-ground"><view v-for="(item, index) in itemSize" :key="index":style="{ borderColor: code.length === index && cursorVisible ? boxActiveColor : boxNormalColor }":class="['box', `box-${type + ''}`, `box::after`]"><view :style="{ borderColor: boxActiveColor }" class="middle-line"v-if="type === 'middle' && !code[index]"></view><text class="code-text">{{ code[index] | codeFormat(isPassword) }}</text></view></view></view>
</template><script>/*** @description 输入验证码组件* @property {string} type = [box|middle|bottom] - 显示类型 默认:box -eg:bottom* @property {string} inputType = [text|number] - 输入框类型 默认:number -eg:number* @property {number} size - 验证码输入框数量 默认:6 -eg:6* @property {boolean} isFocus - 是否立即聚焦 默认:true* @property {boolean} isPassword - 是否以密码形式显示 默认false -eg: false* @property {string} cursorColor - 光标颜色 默认:#cccccc* @property {string} boxNormalColor - 光标未聚焦到的框的颜色 默认:#cccccc* @property {string} boxActiveColor - 光标聚焦到的框的颜色 默认:#000000* @event {Function(data)} confirm - 输入完成回调函数*/import {getElementRect} from './util.js';export default {name: 'verify-code',props: {value: {type: String,default: () => ''},type: {type: String,default: () => 'box'},inputType: {type: String,default: () => 'number'},size: {type: Number,default: () => 6},isFocus: {type: Boolean,default: () => true},isPassword: {type: Boolean,default: () => false},cursorColor: {type: String,default: () => '#cccccc'},boxNormalColor: {type: String,default: () => '#cccccc'},boxActiveColor: {type: String,default: () => '#000000'}},model: {prop: 'value',event: 'input'},data() {return {cursorVisible: false,cursorHeight: 35,code: '', // 输入的验证码codeCursorLeft: [], // 向左移动的距离数组,itemSize: 6,getElement: getElementRect(this),isPatch: false};},created() {this.cursorVisible = this.isFocus;this.validatorSize();},mounted() {this.init();},methods: {/*** 设置验证码框数量*/validatorSize() {if (this.size > 0) {this.itemSize = Math.floor(this.size);} else {throw "methods of 'size' is integer";}},/*** @description 初始化*/init() {this.getCodeCursorLeft();this.setCursorHeight();},/*** @description 计算光标的高度*/setCursorHeight() {this.getElement('.box', 'single', boxElm => {this.cursorHeight = boxElm.height * 0.6;});},/*** @description 获取光标在每一个box的left位置*/getCodeCursorLeft() {// 获取父级框的位置信息this.getElement('#input-ground', 'single', parentElm => {const parentLeft = parentElm.left;// 获取各个box信息this.getElement('.box', 'array', elms => {this.codeCursorLeft = [];elms.forEach(elm => {this.codeCursorLeft.push(elm.left - parentLeft + elm.width / 2);});});});},// 输入框输入变化的回调onInput(e) {let {value,keyCode} = e.detail;this.cursorVisible = value.length < this.itemSize;this.code = value;this.$emit('input', value);this.inputSuccess(value);},// 输入完成回调inputSuccess(value) {if (value.length === this.itemSize && !this.isPatch) {this.$emit('confirm', value);} else {this.isPatch = false;}},// 输入聚焦inputFocus() {this.cursorVisible = this.code.length < this.itemSize;},// 输入失去焦点inputBlur() {this.cursorVisible = false;}},watch: {value(val) {if (val !== this.code) {this.code = val;}}},filters: {codeFormat(val, isPassword) {return val ? (isPassword ? '*' : val) : '';}}};
</script>
<style scoped>.verify-code {position: relative;width: 100%;box-sizing: border-box;}.verify-code .input {height: 100%;width: 200%;position: absolute;left: -100%;z-index: 1;color: transparent;caret-color: transparent;background-color: rgba(0, 0, 0, 0);}.verify-code .cursor {position: absolute;top: 50%;transform: translateY(-50%);display: inline-block;width: 2px;animation-name: cursor;animation-duration: 0.8s;animation-iteration-count: infinite;}.verify-code .input-ground {display: flex;justify-content: space-between;align-items: center;width: 100%;box-sizing: border-box;}.verify-code .input-ground .box {position: relative;display: inline-block;width: 100rpx;height: 140rpx;}.verify-code .input-ground .box-bottom {border-bottom-width: 2px;border-bottom-style: solid;}.verify-code .input-ground .box-box {border-width: 2px;border-style: solid;}.verify-code .input-ground .box-middle {border: none;}.input-ground .box .middle-line {position: absolute;top: 50%;left: 50%;width: 50%;transform: translate(-50%, -50%);border-bottom-width: 2px;border-bottom-style: solid;}.input-ground .box .code-text {position: absolute;top: 50%;left: 50%;font-size: 80rpx;transform: translate(-50%, -50%);}@keyframes cursor {0% {opacity: 1;}100% {opacity: 0;}}
</style>

util.js

/*** @description 获取元素节点 - 大小等信息* @param {string} elm - 节点的id、class 相当于 document.querySelect的参数 -eg: #id* @param {string} type = [single|array] - 单个元素获取多个元素 默认是单个元素* @param {Function} callback - 回调函数* @param {object} that - 上下文环境 vue2:this ,vue3: getCurrentInstance();*/
export const getElementRect = (that) => (elm, type = 'single', callback) => {// #ifndef H5uni.createSelectorQuery().in(that)[type === 'array' ? 'selectAll' : 'select'](elm).boundingClientRect().exec(data => {callback(data[0]);});// #endif// #ifdef H5let elmArr = [];const result = [];if (type === 'array') {elmArr = document.querySelectorAll(elm);} else {elmArr.push(document.querySelector(elm));}for (let elm of elmArr) {result.push(elm.getBoundingClientRect());}console.log('result', result)callback(type === 'array' ? result : result[0]);// #endif
}

相关文章:

【uniapp】短信验证码输入框

需求是短信验证码需要格子输入框 如图 网上找了一个案例改吧改吧 直接上代码 结构 <template><view class"verify-code"><!-- 输入框 --><input id"input" :value"code" class"input" :focus"isFocus"…...

负载均衡的综合部署练习(hproxy+keepalived和lvs-DR+keepalived+nginx+Tomcat)

一、haproxykeepalived haproxy 2台 20.0.0.21 20.0.0.22 nginx 2台 20.0.0.23 20.0.0.24 客户机 1台 20.0.0.30 这里没有haproxy不是集群的概念&#xff0c;他只是代理服务器。 访问他直接可以直接访问后端服务器 关闭防火墙 安装haproxy和环境&#xff1a; yum in…...

设计模式——策略模式(Strategy Pattern)+ Spring相关源码

文章目录 一、策略模式定义二、例子1. 菜鸟教程例子&#xff08;略有改动&#xff09;1.1 、定义。1.2、定义加法策略类1.3、定义乘法策略类1.4、创建 Context 类1.5、使用 2、JDK awt包——BufferStrategy3、Spring源码 —— InstantiatorStrategy4、Spring源码 —— Instanti…...

ORB-SLAM3算法2之开源数据集运行ORB-SLAM3生成轨迹并用evo工具评估轨迹

文章目录 0 引言1 数据和真值1.1 TUM1.2 EuRoc1.3 KITTI2 ORB-SLAM3的EuRoc示例3 ORB-SLAM3的TUM-VI示例4 ORB-SLAM3的ROS各版本示例4.1 单目4.2 单目和IMU4.3 双目4.4 双目和IMU4.5 RGB-D0 引言 ORB-SLAM3算法1 已成功编译安装ORB-SLAM3到本地,本篇目的是用TUM、EuRoc和KITT…...

Qt 序列化函数和反序列化函数

文章目录 界面学生类序列化函数反序列化函数刷新所选择的下拉表值添加 界面 学生类 // 创建学生信息类 class studentInfo { public:QString id; // 学号QString name; // 学生姓名QString age; // 学生年龄// 重写QDataStream& operator<<操作符&…...

Linux之线程池

线程池 线程池概念线程池的应用场景线程池实现原理单例模式下线程池实现STL、智能指针和线程安全其他常见的各种锁 线程池概念 线程池&#xff1a;一种线程使用模式。 线程过多会带来调度开销&#xff0c;进而影响缓存局部性和整体性能。而线程池维护着多个线程&#xff0c;等待…...

MAC安装stable diffusion

./webui.sh --precision full --no-half-vae --disable-nan-check --api Command: "/Users/xxxx/aigc/stable-diffusion-webui/venv/bin/python3" -m pip install torch2.0.1 torchvision0.15.2 Error code: 2 执行命令&#xff1a; pip install torch2.0.1 torchvi…...

FPGA_状态机工作原理

FPGA_状态机介绍和工作原理 状态机工作原理Mealy 状态机模型Moore 状态机模型状态机描述方式代码格式 总结 状态机工作原理 状态机全称是有限状态机&#xff08;Finite State Machine、FSM&#xff09;&#xff0c;是表示有限个状态以及在这些状态之间的转移和动作等行为的数学…...

【python练习】python斐波那契数列超时问题

计算斐波那契数列第n项的数字 Description计算斐波那契数列第n项的数字&#xff0c;其中f(1)f(2)1,f(n)f(n-1)f(n-2)&#xff0c;如1&#xff0c;1&#xff0c;2&#xff0c;3&#xff0c;5,......Input 正整数n(n<100)Output 一个整数f(n)Sample Input 1 8 Sample Output 1…...

SpringCloud 微服务全栈体系(五)

第七章 Feign 远程调用 先来看我们以前利用 RestTemplate 发起远程调用的代码&#xff1a; 存在下面的问题&#xff1a; 代码可读性差&#xff0c;编程体验不统一 参数复杂 URL 难以维护 Feign 是一个声明式的 http 客户端&#xff0c;官方地址&#xff1a;https://github.…...

msvcp140.dll丢失的正确解决方法

在使用电脑中我们经常会遇到一些错误提示&#xff0c;其中之一就是“msvcp140.dll丢失”。这个错误通常会导致某些应用程序无法正常运行。为了解决这个问题&#xff0c;我们需要采取一些措施来修复丢失的msvcp140.dll文件。本文将介绍6个不同的解决方法&#xff0c;帮助读者解决…...

go pprof 如何使用 --chatGPT

gpt: pprof 是 Go 语言的性能分析工具&#xff0c;它可以用来检测 CPU 使用情况、内存使用情况、以及阻塞情况。你可以使用 pprof 来帮助诊断程序的性能问题&#xff0c;包括内存泄漏。 以下是如何使用 pprof 来分析内存泄漏的基本步骤&#xff1a; 1. **导入 pprof 包**&am…...

大数据可视化BI分析工具Apache Superset实现公网远程访问

大数据可视化BI分析工具Apache Superset实现公网远程访问 文章目录 大数据可视化BI分析工具Apache Superset实现公网远程访问前言1. 使用Docker部署Apache Superset1.1 第一步安装docker 、docker compose1.2 克隆superset代码到本地并使用docker compose启动 2. 安装cpolar内网…...

软考系统架构师知识点集锦二:软件工程

一、考情分析 二、考点精讲 2.1 软件过程模型 &#xff08;1&#xff09;原型模型 典型的原型开发方法模型。适用于需求不明确的场景,可以帮助用户明确需求。可以分为[抛弃型原型]与[演化型原型] 原型模型两个阶段: 1、原型开发阶段;2、目标软件开发阶段。 &#x…...

Go并发:使用sync.Pool来性能优化

简介 在Go提供如何实现对象的缓存池功能&#xff1f;常用一种实现方式是&#xff1a;sync.Pool, 其旨在缓存已分配但未使用的项目以供以后重用&#xff0c;从而减轻垃圾收集器&#xff08;GC&#xff09;的压力。 快速使用 sync.Pool的结构也比较简单&#xff0c;常用的方法…...

git stash的使用方法

git stash的使用方法 应用场景 当我们在开发一个新功能的时候&#xff0c;或者开发到一半&#xff0c;然后就收到了线上master 出现了bug&#xff0c;当分支开发已经进行了或者进行到一半了&#xff0c;这时怎么办呢&#xff1f; 这时解决方案有两种&#xff1a;一种是先先将当…...

【影刀演示_发送邮件的格式化HTML留存】

发送邮件的格式化HTML留存 纯文本&#xff1a; 亲爱的小张: 端午节将至&#xff0c;公司为了感谢大家一年以来的辛勤工作和付出&#xff0c;特别为大家准备了京客隆超市福利卡&#xff0c;希望为大家带来些许便利和节日的喜悦。 以下是您的福利卡卡号和密码&#xff0c;请您…...

深度学习(4)---生成式对抗网络(GAN)

文章目录 一、原理讲述1.1 概念讲解1.2 生成模型和判别模型 二、训练过程2.1 训练原理2.2 损失函数 三、应用 一、原理讲述 1.1 概念讲解 1. 生成式对抗网络&#xff08;Generative Adversarial Network&#xff0c;GAN&#xff09;是一种深度学习模型&#xff0c;是近年来复杂…...

ThinkPad电脑HDMI接口失灵如何解决?

ThinkPad电脑HDMI接口失灵如何解决&#xff1f; 如果平时正常使用的外接显示器&#xff0c;某天突然无法使用了&#xff0c;重新插拔依然无信号的话&#xff0c;可以打开系统的设备管理器&#xff08;快捷键winx&#xff09;&#xff0c;首先看一下监视器的识别情况&#xff0c…...

第四部分:JavaScript

一&#xff1a;jQuery 1.1&#xff1a;jQuery介绍 什么是jQuery&#xff1f; jQuery是JavaScript和查询&#xff08;Query&#xff09;&#xff0c;它是辅助JavaScript开发的js类库 jQuery的核心思想 核心思想是write less&#xff0c;do more&#xff0c;所以它实现了很多浏览…...

label-studio的使用教程(导入本地路径)

文章目录 1. 准备环境2. 脚本启动2.1 Windows2.2 Linux 3. 安装label-studio机器学习后端3.1 pip安装(推荐)3.2 GitHub仓库安装 4. 后端配置4.1 yolo环境4.2 引入后端模型4.3 修改脚本4.4 启动后端 5. 标注工程5.1 创建工程5.2 配置图片路径5.3 配置工程类型标签5.4 配置模型5.…...

树莓派超全系列教程文档--(61)树莓派摄像头高级使用方法

树莓派摄像头高级使用方法 配置通过调谐文件来调整相机行为 使用多个摄像头安装 libcam 和 rpicam-apps依赖关系开发包 文章来源&#xff1a; http://raspberry.dns8844.cn/documentation 原文网址 配置 大多数用例自动工作&#xff0c;无需更改相机配置。但是&#xff0c;一…...

相机Camera日志实例分析之二:相机Camx【专业模式开启直方图拍照】单帧流程日志详解

【关注我&#xff0c;后续持续新增专题博文&#xff0c;谢谢&#xff01;&#xff01;&#xff01;】 上一篇我们讲了&#xff1a; 这一篇我们开始讲&#xff1a; 目录 一、场景操作步骤 二、日志基础关键字分级如下 三、场景日志如下&#xff1a; 一、场景操作步骤 操作步…...

Frozen-Flask :将 Flask 应用“冻结”为静态文件

Frozen-Flask 是一个用于将 Flask 应用“冻结”为静态文件的 Python 扩展。它的核心用途是&#xff1a;将一个 Flask Web 应用生成成纯静态 HTML 文件&#xff0c;从而可以部署到静态网站托管服务上&#xff0c;如 GitHub Pages、Netlify 或任何支持静态文件的网站服务器。 &am…...

OkHttp 中实现断点续传 demo

在 OkHttp 中实现断点续传主要通过以下步骤完成&#xff0c;核心是利用 HTTP 协议的 Range 请求头指定下载范围&#xff1a; 实现原理 Range 请求头&#xff1a;向服务器请求文件的特定字节范围&#xff08;如 Range: bytes1024-&#xff09; 本地文件记录&#xff1a;保存已…...

【2025年】解决Burpsuite抓不到https包的问题

环境&#xff1a;windows11 burpsuite:2025.5 在抓取https网站时&#xff0c;burpsuite抓取不到https数据包&#xff0c;只显示&#xff1a; 解决该问题只需如下三个步骤&#xff1a; 1、浏览器中访问 http://burp 2、下载 CA certificate 证书 3、在设置--隐私与安全--…...

均衡后的SNRSINR

本文主要摘自参考文献中的前两篇&#xff0c;相关文献中经常会出现MIMO检测后的SINR不过一直没有找到相关数学推到过程&#xff0c;其中文献[1]中给出了相关原理在此仅做记录。 1. 系统模型 复信道模型 n t n_t nt​ 根发送天线&#xff0c; n r n_r nr​ 根接收天线的 MIMO 系…...

Linux 下 DMA 内存映射浅析

序 系统 I/O 设备驱动程序通常调用其特定子系统的接口为 DMA 分配内存&#xff0c;但最终会调到 DMA 子系统的dma_alloc_coherent()/dma_alloc_attrs() 等接口。 关于 dma_alloc_coherent 接口详细的代码讲解、调用流程&#xff0c;可以参考这篇文章&#xff0c;我觉得写的非常…...

2025.6.9总结(利与弊)

凡事都有两面性。在大厂上班也不例外。今天找开发定位问题&#xff0c;从一个接口人不断溯源到另一个 接口人。有时候&#xff0c;不知道是谁的责任填。将工作内容分的很细&#xff0c;每个人负责其中的一小块。我清楚的意识到&#xff0c;自己就是个可以随时替换的螺丝钉&…...

起重机起升机构的安全装置有哪些?

起重机起升机构的安全装置是保障吊装作业安全的关键部件&#xff0c;主要用于防止超载、失控、断绳等危险情况。以下是常见的安全装置及其功能和原理&#xff1a; 一、超载保护装置&#xff08;核心安全装置&#xff09; 1. 起重量限制器 功能&#xff1a;实时监测起升载荷&a…...