uniapp自定义验证码输入框,隐藏光标

news2024/9/22 11:37:45

一. 前言

先看下使用场景效果图:

  1. 点击输入框唤起键盘,蓝框就相当于input的光标,验证码输入错误或者不符合格式要求会将字体以及边框改成红色提示,持续1s,然后清空数据,恢复原边框样式;
  2. 5位验证码输入完毕,点击页面其他位置,隐藏键盘;这时如果发现验证码有误,再次点击输入框又唤起键盘,也能正常删除数字(这里其实做的时候遇到了bug,再次聚焦不能删除错误数字,下文会讲到)。
    在这里插入图片描述

二. 实现思路

具体实现思路:

  1. 将input标签相对于父元素做绝对定位,与父元素左边距设置为负的本身宽度即可(position: absolute; top: 0; left:-100%; width: 100%; height: 100%;)。
  2. 动态去设置input的focus属性。
  3. input同级使用for循环去创建5个正方形的view标签。
  4. 给input同级创建的view标签绑定点击事件,在点击事件方法实现中去设置input的focus属性为true,即可弹出键盘。
  5. 在键盘输入的时候,即可触发input属性的一系列方法,利用v-model双向绑定,将input输入的值赋值给循环的view方框即可。
    这样input也就不在屏幕中,但是又可以触发input的事件。

总的来说就是,使用for循环去创建5个正方形的view标签,然后创建一个input标签,type=tel,最大输入长度为5(根据需求来设置),再将input伪隐藏掉,获取的值分别放到5个view中展示。

验证码失败后利用v-model双向绑定,清空输入的值,增加错误提示文字和边框样式。

三. 代码实现

父组件

<uni-popup ref="codeInputPopup" background-color="#fff" :mask-click ="false" type="center">
     <CodeInput
	  :codeLength="5"
	  :disabled="codeBtnDisabled"
	  @codeInputClose="codeInputClose"
	  @submitGoodCode="submitGoodCode"
	 />
</uni-popup>
<script>
export default {
  data() {
    return {
     	intviation_code:'', //邀请码
		codeBtnDisabled: false //防止接口请求还未返回数据,用户多次点击
    }
  },
  methods: {
    // 提交邀请码
	async submitGoodCode(intviation_code){
		this.codeBtnDisabled = true
		this.intviation_code = intviation_code
		
		const response =  await this.$api.post('/ebapi/pink_api/secret_intviation_check', {
		  code: intviation_code
		})
		if(response.code === 200){
			this.codeBtnDisabled = false
			this.$refs.codeInputPopup.close()
		}else{
			this.codeBtnDisabled = false
			this.$refs.codeInputPopup.close()
			this.$api.msg(response.msg)
		 }
		},
	codeInputClose(){
	  this.$refs.codeInputPopup.close()
	  this.codeBtnDisabled = false
	}
}
</script>

子组件

<template>
  <view>
    <view class="code-popup-top">
      <view class="code-title">请输入商品邀请码</view>
      <view class="close-icon" @click="codeInputClose">
        <uni-icons type="closeempty" size="30" color="#999999" />
      </view>
    </view>
    <!-- 错误提示 -->
    <view class="code_errow" v-if="codeColor == '#ff0000'&& !isNum">邀请码必须{{ codeLength }}位数</view>
    <view class="code_errow" v-if="codeColor == '#ff0000'&& isNum ">邀请码必须是数字</view>
    <view class="code_input_con">
      <view
        v-for="(item, index) in codeLength"
        :key="index"
        class="code_input_item"
        :style="(index == intviation_code.length? 'border: 5rpx solid #1195db; width: 88rpx; height: 88rpx; line-height: 80rpx;':'color: ' + codeColor + ';' +'border: 2rpx solid' + codeColor)"
        @click="focus = true"
      >{{ intviation_code[index] && intviation_code[index] || '' }}</view>
      <input
        class="cinput"
        type="tel"
        v-model="intviation_code"
        :maxlength="codeLength"
        :focus="focus"
        :cursor="intviation_code.length"
        @focus="focus = true "
        @blur="focus = false"
      />
    </view>
    <button
      :class="['submit_code_btn', disabled ? 'btn_disabled' : '']"
      :disabled="disabled"
      @click="submitGoodCode"
    >确定</button>
  </view>
</template>

<script>
export default {
  data() {
    return {
      codeColor: '#313131', //自定义错误码颜色
      intviation_code: '', //用户输入的验证码
      focus: false, // 动态获取焦点的值
      isNum: false,
    }
  },
  props: {
    codeLength: {
      type: Number,
      default: 5,
    },
    disabled: {
      type: Boolean,
      default: false,
    },
  },
  methods: {
    codeInputClose() {
      this.intviation_code = ''
      this.$emit('codeInputClose')
    },
    submitGoodCode() {
      if (this.intviation_code.length === this.codeLength) {
        if (Number(this.intviation_code)) {
          this.$emit('submitGoodCode', this.intviation_code)
        } else {
          this.isNum = true
          this.publicErrorSetting()
        }
      } else {
        this.publicErrorSetting()
      }
    },
    // 输入不符合规范,更改样式并清空
    publicErrorSetting() {
      this.codeColor = '#ff0000'
      setTimeout(() => {
        this.intviation_code = ''
        this.codeColor = '#313131'
        this.isNum = false
      }, 1000)
    },
  },
}
</script>

<style lang="scss" scoped>
.code-popup-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 50upx;
  .code-title {
    font-size: 34upx;
    color: #333;
    font-weight: bold;
    position: relative;
    &::before {
      content: '';
      position: absolute;
      bottom: 0;
      width: 40upx;
      height: 19upx;
      background: linear-gradient(
        to right,
        rgba(57, 181, 74, 1),
        rgba(57, 181, 74, 0.1)
      );
    }
  }
  .close-icon {
    background: #f2f4f7;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
  }
}
.code_errow {
  font-size: 30upx;
  color: #ff5500;
  margin-bottom: 20upx;
}
.submit_code_btn {
  width: 100%;
  height: 83upx;
  line-height: 83upx;
  border-radius: 7upx;
  background: #39b54a;
  color: #fff;
  font-size: 31upx;
  text-align: center;
  margin-top: 45upx;
}
.btn_disabled {
  color: rgba(255, 255, 255, 0.5) !important;
  background-color: rgba(57, 181, 74, 0.4) !important;
}
.code_input_con {
  display: flex;
  justify-content: space-around;
  position: relative;
  .code_input_item {
    margin-left: 10upx;
    text-align: center;
    line-height: 88upx;
    border-radius: 14upx;
    width: 88upx;
    height: 88upx;
    font-size: 60upx;
    font-weight: bold;
    color: #333;
    &:last-child {
      margin-right: 0;
    }
  }
  /*input隐藏掉*/
  .cinput {
    position: absolute;
    top: 0;
    left: -100%; 
    width: 100%;
    height: 100%;
  }
}
</style>

四. 过程中遇到的问题

1)input 的type=‘number’, ios手机正常,光标在内容最后,但Android手机光标有时候在内容最前面,导致聚焦内容删不掉。

修改input 的type = 'tel':cursor="intviation_code.length", 这样cursor属性才生效,并指定focus时光标的位置在内容最后;
type=‘tel’,也会有个小问题,可以输入一些字符,但是我们的需求只能是数字,所以代码中要做限制。就能解决这个问题了。

这个cursor无效的问题,在h5模式应该是type的原因,我试了在type是number或digit时cursor就无效,text、tel、idcard就有效

2)还有另外一种方法

  1. 设置input的type=“number”,就不需要设置光标位置了;然后隐藏input文字和光标,相当于间接隐藏了input框;
  2. 用到了css样式设置,color: transparent; caret-color: transparent;
  3. 最主要的还是相对于父元素做绝对定位,与父元素左边距设置为负的本身宽度的一半即可(position: absolute; top: 0; left:-100%; width: 200%; height: 100%;)with: 200%为了增大点击区域,解决Android机型再次唤起键盘不能聚焦,删不掉错误数字的问题。

张鑫旭的CSS改变插入光标颜色caret-color简介及其它变色方法
自我测试CSS : caret-color

<template>
  <view>
      <input
        class="cinput"
        type="number"
        v-model="intviation_code"
        :maxlength="codeLength"
        :focus="focus"
        @focus="focus = true "
        @blur="focus = false"
      />
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      intviation_code: '', //商品邀请码
      focus: false,
    }
  },
  methods: {}
</script>

<style lang="scss" scoped>
.cinput {
    position: absolute;
    top: 0;
    left: -100%;
    width: 200%;
    height: 100%;
    color: transparent;  //输入文字颜色透明
    caret-color: transparent !important; //改变插入光标颜色为透明
  }
  }
  // 考虑兼容性
  // 浏览器支持caret-color属性,优先使用caret-color(Chrome/Firefox/Opera);其次使用::first-line方法(Safari);最后忽略(如IE)。
  @supports (-webkit-mask: none) and (not (caret-color: transparent)) {
    .cinput {
      color: transparent !important;
    }
    .cinput::first-line {
      color: transparent !important;
    }
  }
</style>

还可参考:
6位验证码输入框、隐藏光标、letter-spacing失效、自定义光标,光标动画
uniapp 手机验证码输入框(随机数、倒计时、隐藏手机号码中间四位)可以直接使用

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【Kubernetes】【十二】Pod详解 Pod调度

Pod调度 ​ 在默认情况下&#xff0c;一个Pod在哪个Node节点上运行&#xff0c;是由Scheduler组件采用相应的算法计算出来的&#xff0c;这个过程是不受人工控制的。但是在实际使用中&#xff0c;这并不满足的需求&#xff0c;因为很多情况下&#xff0c;我们想控制某些Pod到达…

Prometheus监控案例-tomcat、mysql、redis、haproxy、nginx

监控tomcat tomcat自身并不能提供监控指标数据&#xff0c;需要借助第三方exporter实现&#xff1a;https://github.com/nlighten/tomcat_exporter 构建镜像 基于tomcat官方镜像&#xff0c;重新制作一个镜像&#xff0c;将tomcat-exporter和tomcat整合到一起。Ddockerfile如…

【安全知识】——如何绕过cdn获取真实ip

作者名&#xff1a;白昼安全主页面链接&#xff1a; 主页传送门创作初心&#xff1a; 以后赚大钱座右铭&#xff1a; 不要让时代的悲哀成为你的悲哀专研方向&#xff1a; web安全&#xff0c;后渗透技术每日鸡汤&#xff1a; 现在的样子是你想要的吗&#xff1f;cdn简单来说就是…

商标侵权行为的种类有哪些

商标侵权行为的种类有哪些 1、商标侵权行为的种类有以下七种&#xff1a; (1)未经商标注册人的许可&#xff0c;在同一种商品上使用与其注册商标相同的商标的; (2)未经商标注册人的许可&#xff0c;在同一种商品上使用与其注册商标近似的商标&#xff0c;或者在类似商品上使…

Python3 面向对象实例及演示

Python从设计之初就已经是一门面向对象的语言&#xff0c;正因为如此&#xff0c;在Python中创建1个类和对象是很容易的。本章节我们将详细介绍Python的面向对象编程。 如果以前没有接触过面向对象的编程语言&#xff0c;那可能需要先了解一些面向对象语言的一些基本特征&…

SPI+DMA传输性能比较

本文章仅仅简单记录32单片机的SPIDMA驱动显示屏的性能测试&#xff0c;这里不花费时间介绍SPI和DMA。 硬件材料&#xff1a;SPI显示屏一个&#xff0c;32单片机 软件材料&#xff1a; 1.LCD的SPI驱动显示程序&#xff08;SPI / SPIDMA&#xff09;&#xff1a; &#xff08;1&a…

葡萄酒(WINE)数据集分类(PyTorch实现)

一、数据集介绍 Data Set Information: These data are the results of a chemical analysis of wines grown in the same region in Italy but derived from three different cultivars. The analysis determined the quantities of 13 constituents found in each of …

QML debugging is enabled. Only use this in a safe environment.

系列文章目录 文章目录系列文章目录前言一、Qt Quick是什么1.QML核心二、使用步骤1.main.cpp3.运行结果前言 因为有个需求&#xff1a;C和web交互&#xff0c;初步想到在Qt中使用QWebChannel 今天第一次使用Qt Qml&#xff0c;建了qt Quick工程 一、Qt Quick是什么 QML&…

代码随想录算法训练营第三十五天 | 435. 无重叠区间,763.划分字母区间,56. 合并区间

一、参考资料无重叠区间 https://programmercarl.com/0435.%E6%97%A0%E9%87%8D%E5%8F%A0%E5%8C%BA%E9%97%B4.html 划分字母区间https://programmercarl.com/0763.%E5%88%92%E5%88%86%E5%AD%97%E6%AF%8D%E5%8C%BA%E9%97%B4.html 合并区间https://programmercarl.com/0056.%E5%90…

分享111个HTML艺术时尚模板,总有一款适合您

分享111个HTML艺术时尚模板&#xff0c;总有一款适合您 111个HTML艺术时尚模板下载链接&#xff1a;https://pan.baidu.com/s/1sYo2IPma4rzeku3yCG7jGw?pwdk8dx 提取码&#xff1a;k8dx Python采集代码下载链接&#xff1a;采集代码.zip - 蓝奏云 时尚理发沙龙服务网站模…

实现8086虚拟机(二)——模拟CPU和内存

文章目录CPU 架构EU&#xff08;执行单元&#xff09;BIU&#xff08;总线接口单元&#xff09;小结一下模拟内存模拟 BIU模拟 EU模拟 CPU总结要模拟 8086 CPU 运行&#xff0c;必须知道 CPU 的一些知识。下文的知识点都来自《Intel_8086_Family_Users_Manual 》。CPU 架构 微…

spring之Spring AOP基于注解

文章目录前言一、Spring AOP基于注解的所有通知类型1、前置通知2、后置通知3、环绕通知4、最终通知5、异常通知二、Spring AOP基于注解之切面顺序三、Spring AOP基于注解之通用切点三、Spring AOP基于注解之连接点四、Spring AOP基于注解之全注解开发前言 通知类型包括&#x…

J4、哨兵集群、redis切片还是加实例

哨兵集群 单节点的哨兵还是可能会发生故障&#xff0c;需要部署集群。 部署哨兵时&#xff0c;只需要下面的命令&#xff0c;那哨兵是如果互相发现的&#xff1f; sentinel monitor <master-name> <ip> <redis-port> <quorum> 基于 pub/sub 机制的哨…

Premiere基础操作

一&#xff1a;设置缓存二&#xff1a;ctrI导入素材三&#xff1a;导入图像序列四&#xff1a;打开吸附。打开吸附后素材会对齐。五&#xff1a;按~键可以全屏窗口。六&#xff1a;向前选择轨道工具。在时间线上点击&#xff0c;向前选中时间线上素材。向后选择轨道工具&#x…

19_微信小程序之优雅实现侧滑菜单

19_微信小程序之优雅实现侧滑菜单一.先上效果图 要实现这样一个效果&#xff0c;布局其实很简单&#xff0c;整体布局是一个横向滚动的scroll-view&#xff0c;难点在于怎么控制侧滑菜单的回弹&#xff0c;以及寻找回弹的边界条件? 此篇文章主要是基于uni-app来实现的&#xf…

MySQL 记录锁+间隙锁可以防止删除操作而导致的幻读吗?

文章目录什么是幻读&#xff1f;实验验证加锁分析总结什么是幻读&#xff1f; 首先来看看 MySQL 文档是怎么定义幻读&#xff08;Phantom Read&#xff09;的: The so-called phantom problem occurs within a transaction when the same query produces different sets of r…

使用Fairseq进行Bart预训练

文章目录前言环境流程介绍数据部分分词部分预处理部分训练部分遇到的问题问题1可能遇到的问题问题1问题2前言 本文是使用 fairseq 做 Bart 预训练任务的踩坑记录huggingface没有提供 Bart 预训练的代码 facebookresearch/fairseq: Facebook AI Research Sequence-to-Sequence…

字符串匹配 - 文本预处理:后缀树(Suffix Tree)

上述字符串匹配算法(朴素的字符串匹配算法, KMP 算法, Boyer-Moore算法)均是通过对模式&#xff08;Pattern&#xff09;字符串进行预处理的方式来加快搜索速度。对 Pattern 进行预处理的最优复杂度为 O(m)&#xff0c;其中 m 为 Pattern 字符串的长度。那么&#xff0c;有没有…

windows环境下,vue启动项目后打开chrome浏览器

前言&#xff1a;关于vue启动后打开chrome浏览器&#xff0c;我查了很多资料&#xff0c;方案如下&#xff1a; 1、增加环境变量BROWSER为chrome&#xff08;试了没效果&#xff09; 2、设置系统的默认浏览器为chrome&#xff08;应该可以&#xff0c;但没试&#xff1b;因为…

有序表的应用:[Leetcode 327] 区间和的个数

一、题目 1、题目描述 给你一个整数数组 nums 以及两个整数 lower 和 upper 。求数组中&#xff0c;值位于范围 [lower, upper] &#xff08;包含 lower 和 upper&#xff09;之内的 区间和的个数 。 区间和 S(i, j)表示在 nums 中&#xff0c;位置从 i 到 j 的元素之和&…