【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
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/173279.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

加解密原理(HCIA)

一、加密技术 1、加密的两个核心组件 2、加密技术作用&#xff1a; 二、加解密技术原理 1、对称加密 2、非对称加密 &#xff08;1&#xff09;思考问题&#xff1f; 1&#xff09;、有了非对称加密为什么还用对称加密&#xff1f; 2&#xff09;、如何传递秘钥呢&…

C++标准模板(STL)- 类型支持 (类型特性,is_union,is_class,is_function)

类型特性 类型特性定义一个编译时基于模板的结构&#xff0c;以查询或修改类型的属性。 试图特化定义于 <type_traits> 头文件的模板导致未定义行为&#xff0c;除了 std::common_type 可依照其所描述特化。 定义于<type_traits>头文件的模板可以用不完整类型实例…

Docker 入门

What - 什么是容器 容器是一种轻量级、可移植、自包含的软件打包技术&#xff0c;使应用程序可以在几乎任何地方以相同的方式运行。开发人员在自己笔记本上创建并测试好的容器&#xff0c;无须任何修改就能够在生产系统的虚拟机、物理服务器或公有云主机上运行。容器与虚拟机谈…

尚硅谷大数据项目《在线教育之实时数仓》笔记004

视频地址&#xff1a;尚硅谷大数据项目《在线教育之实时数仓》_哔哩哔哩_bilibili 目录 第8章 数仓开发之DIM层 P024 P025 P026 P027 P028 P029 P030 第8章 数仓开发之DIM层 P024 package com.atguigu.edu.realtime.app.func;import com.alibaba.druid.pool.DruidDat…

网络流学习笔记

网络流基础 基本概念 源点&#xff08;source&#xff09; s s s&#xff0c;汇点 t t t。 容量&#xff1a;约等于边权。不存在的边流量可视为 0 0 0。 ( u , v ) (u,v) (u,v) 的流量通常记为 c ( u , v ) c(u,v) c(u,v)&#xff08;capacity&#xff09;。 流&#xff…

Vue项目搭建及使用vue-cli创建项目、创建登录页面、与后台进行交互,以及安装和使用axios、qs和vue-axios

目录 1. 搭建项目 1.1 使用vue-cli创建项目 1.2 通过npm安装element-ui 1.3 导入组件 2 创建登录页面 2.1 创建登录组件 2.2 引入css&#xff08;css.txt&#xff09; 2.3 配置路由 2.5 运行效果 3. 后台交互 3.1 引入axios 3.2 axios/qs/vue-axios安装与使用 3.2…

SpringCloud 微服务全栈体系(五)

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

【C++】缺省参数及函数重载

&#x1f4d9; 作者简介 &#xff1a;RO-BERRY &#x1f4d7; 学习方向&#xff1a;致力于C、C、数据结构、TCP/IP、数据库等等一系列知识 &#x1f4d2; 日后方向 : 偏向于CPP开发以及大数据方向&#xff0c;欢迎各位关注&#xff0c;谢谢各位的支持 目录 1. 缺省参数1.1 缺省…

迭代器的封装与反向迭代器

一、反向迭代器 在list模拟实现的过程中&#xff0c;第一次接触了迭代器的封装&#xff0c;将list的指针封装成了一个新的类型&#xff0c;并且以迭代器的基本功能对其进行了运算符重载 反向迭代器是对正向迭代器的封装&#xff0c;并且体现了泛型编程的思想&#xff0c;任意…

win10虚拟机安装教程

目录 1、安装VMware 10、12、16都可以&#xff0c;看个人选择 2、开始安装系统&#xff08;以vm16为例&#xff09; 3、在虚拟机中安装win10 完成 1、安装VMware 10、12、16都可以&#xff0c;看个人选择 下面链是我虚拟机安装包&#xff0c;需要可以下载。 YR云盘 软件安…

华为OD机考算法题:高效的任务规划

题目部分 题目高效的任务规划难度难题目说明 你有 n 台机器编号为 1 ~ n&#xff0c;每台都需要完成一项工作&#xff0c; 机器经过配置后都能独立完成一项工作。 假设第 i 台机器你需要花 分钟进行设置&#xff0c; 然后开始运行&#xff0c; 分钟后完成任务。 现在&#x…

虚拟机构建部署单体项目及前后端分离项目

目录 一.部署单体项目 1.远程数据库 1.1远程连接数据库 1.2 新建数据库运行sql文件 2.部署项目到服务器中 3.启动服务器运行 二.部署前后端分离项目 1.远程数据库和部署到服务器 2.利用node环境启动前端项目 3.解决主机无法解析服务器localhost问题 方法一 ​编辑 方…

Illustrator 2024(AI v28.0)

Illustrator 2024是一款功能强大的矢量图形编辑软件&#xff0c;由Adobe公司开发。它是设计师、艺术家和创意专业人士的首选工具&#xff0c;用于创建和编辑各种矢量图形、插图、图标、标志和艺术作品。 以下是Adobe Illustrator的主要功能和特点&#xff1a; 矢量图形编辑&…

命令模式——让程序舒畅执行

● 命令模式介绍 命令模式&#xff08;Command Pattern&#xff09;&#xff0c;是行为型设计模式之一。命令模式相对于其他的设计模式来说并没有那么多条条框框&#xff0c;其实并不是一个很“规矩”的模式&#xff0c;不过&#xff0c;就是基于一点&#xff0c;命令模式相对于…

实战授权码登录流程

我是经常阅读公众号优质文章&#xff0c;也经常体验到公众号的授权登录功能。今天就来实现下&#xff0c;流程图如下 效果图 后端接口 主要用来接收微信服务器推送的公众号用户触发的事件、生成和验证授权码的有效性 解析微信服务器推送的事件通知 public String login(Se…

MySQL 概述 数据库表操作 数据增删改

目录 MySQL概述前言安装与配置MySQL登录与卸载 数据模型概述SQL简介SQL通用语法简介SQL分类 数据库设计(数据库操作)-DDL数据库操作查询数据库 show databases、select database()创建数据库 create database使用数据库 use删除数据库 drop database 图形化工具连接数据库操作数…

Node.js中的单线程服务器

为了解决多线程服务器在高并发的I/O密集型应用中的不足&#xff0c;同时避免早期简单单线程服务器的性能障碍&#xff0c;Node.js采用了基于"事件循环"的非阻塞式单线程模型&#xff0c;实现了如下两个目标&#xff1a; &#xff08;1&#xff09;保证每个请求都可以…

通过el-tree 懒加载树,创建国家地区四级树

全国四级行政地区树数据库sql下载路径&#xff1a;【免费】全国四级地区(省市县)数据表sql资源-CSDN文库https://download.csdn.net/download/weixin_51722520/88469807?spm1001.2014.3001.5503 我在后台获取地区信息添加了限制&#xff0c;只获取parentid为当前的地…

[AUTOSAR][诊断管理][ECU][$34] 下载请求

文章目录 一、简介二、服务请求报文定义肯定响应支持的NRC三、示例代码34_req_dowload.c一、简介 RequestDownload(0x34)—— 下载请求 这个服务主要是用来给ECU下载数据的,最常见的应用就是在bootloader中,程序下载工具会发起下载请求,以完成ECU程序的升级。 二、服务…

计算线阵相机 到 拍摄产品之间 摆放距离?(隐含条件:保证图像不变形)

一物体被放置在传送带上&#xff0c;转轴的直径为100mm。已知线阵相机4K7u&#xff08;一行共4096个像素单元&#xff0c;像素单元大小7um&#xff09;&#xff0c;镜头35mm&#xff0c;编码器2000脉冲/圈。保证图像不变形的条件下&#xff0c;计算相机到产品之间 摆放距离&…