input输入框模拟验证码输入效果

article/2025/8/28 10:55:48

今天看到一个帖子,说到用input输入框模拟滴滴、摩拜等app验证码输入效果,提到了一个方案:

1、利用input来获得焦点,自动调用手机的数字键盘

2、实际将输入框用透明度隐藏

3、用label的for属性与input联动来显示输入的数字

于是照着这个思路实际操作了一番,还是发现了不少问题。

首先页面布局:

<div class="container"><h2>输入验证码:</h2><div class="vcode" id='vertifycode'><input type="tel" maxlength='6' ref='code' class='code' id='code'><div class="labels"><label class='label' for="code"></label><label class='label' for="code"></label><label class='label' for="code"></label><label class='label' for="code"></label><label class='label' for="code"></label><label class='label' for="code"></label></div></div>
</div>

样式:

@keyframes animate {100% {opacity: 0;}}.container{padding: 5%;}.vcode{position: relative;width: 100%;overflow: hidden;}.code{width: 100%;padding: 0;height: 40px;font-size: 35px;overflow: hidden;border: none;outline: none;opacity: 0;margin-left: -100%; // ios上透明度为0时依然有光标-webkit-tap-highlight-color: transparent;}.labels{display: flex;display: -webkit-flex;width: 100%;height: 40px;justify-content: space-between;-webkit-justify-content: space-between;margin-top: -40px;-webkit-tap-highlight-color: transparent;// 解决ios点击灰色阴影的问题}.label{height: 34px;width: 12%;border-bottom: solid 2px #313131;float: left;color: #313131;font-size: 35px;text-align: center;padding-bottom: 4px;}.active:after{ // 伪类实现光标效果content: ' ';display: inline-block;height: 100%;width: 2px;background: #313131;animation: .8s animate infinite;}

注意点:

1、ios手机上input和lable都会出现点击有灰色背景闪动的问题,所以一定要给样式加上

-webkit-tap-highlight-color: transparent;

2、输入框的默认光标隐藏了后,用伪类实现光标效果:

.active:after{ // 伪类实现光标效果content: ' ';display: inline-block;height: 100%;width: 2px;background: #313131;animation: .8s animate infinite;}

3、ios上虽然input透明度已经设置为0了,但是当其获得焦点时,默认的光标依然存在


可以利用margin将整个input输入框隐藏来解决:

margin-left: -100%; 

样式写好了,就改进行逻辑处理了:

引入vue.js(vue确实好用)

<script src='./js/vue.min.js'></script>

更改页面布局:

<div class="container"><h2>输入验证码:</h2><div class="vcode" id='vertifycode'><input type="tel" maxlength='6' ref='code' class='code' id='code'@focus='focus=true'v-model='code'@blur='focus=false':disabled='disabled'><div class="labels"><label class='label' for="code":class='{active: focus===true && index===currentIndex}'v-for='item,index in length'v-text='arrCode[index]'></label></div></div>
</div>

逻辑代码:

new Vue({el: '#vertifycode',data: {length: 6,currentIndex: 0,code: '',focus: false,arrCode: [],disabled: false},computed: {arrCode: function () {return this.code.split('');},currentIndex: function () {return this.code.length;}},watch: {code: function (newV,oldV) {var self = this;this.code = newV.replace(/[^\d]/g,''); // 限制非数字console.log(newV);if(newV.length >= 6) {this.disabled = true;this.$refs.code.blur();setTimeout(function () {alert('submit:'+self.code+'');},500);}}}

数据说明:

length: 密码长度,根据实际需求设定;

currentIndex: 当前获得焦点的label的索引;

code: 输入的内容;

focus: 判断输入框的焦点状况;

arrCode:用于填充label标签内容;

disabled:控制输入框是否可用,输入6位验证码后禁用。


当输入长度等于6时,输入框失去焦点,自动提交数据,弹出提示框,实际效果:

         









http://chatgpt.dhexx.cn/article/CVU7lNnB.shtml

相关文章

vue实现验证码输入

需求&#xff1a;这种样式的验证码输入&#xff0c;进入页面时光标停留在第一个格子&#xff0c;随着输入的数字向后移动。 实现逻辑&#xff1a; ①首先考虑样式&#xff0c;将六个格子及获取光标的样式写出来。 ②需要一个input框作为输入&#xff0c;所以在页面上加input框&…

爬虫验证码-手动输入

一般网站登录的时候会有验证码的问题,下面是将验证码下载到本地,手动输入模拟登录。在请求的时候使用会话是为了保证获取的验证码、表单令牌等数据一致 import requestsfrom bs4 import BeautifulSoupheaders = {User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWeb…

小程序多个输入框输入验证码功能 实现

老套路,先上图 <view class"check"><!-- 输入核验码 --><view class"query"><view class"query_item_name">请输入核验码</view><view class"query_num_block"><input typenumber class"…

手机计算机上输入错误是什么意思,电脑输入验证码总是提示错误该怎么解决?...

不少同学都遇到过在页面中输入验证码总是错误的问题&#xff0c;并且不管更换几次验证码图片&#xff0c;依然提示验证码错误&#xff0c;着实影响心情&#xff0c;接下来小编总结了一部分造成验证码总是错误的原因以及解决办法&#xff0c;希望对大家有所帮助; 第一种&#xf…

Python模拟登陆古诗文网手动输入验证码显示验证码错误

Python模拟登陆古诗文网手动输入验证码显示验证码错误 import requests from lxml import etree sessionrequests.Session() headers {User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36,}…

自定义验证码输入框:VerificationCodeView

先上两张效果图&#xff1a; 1.java类&#xff1a; package com...ui;import android.annotation.SuppressLint; import android.app.Activity; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Color; import android.text…

案例:登录中输入验证码(Session及JSP技术应用)

案例&#xff1a;登录中输入验证码(会话技术) 1.案例需求&#xff1a;1.访问带有验证码的登录页面login.jsp2.用户输入用户名&#xff0c;密码以及验证码如果用户和密码输入有误&#xff0c;跳转登录页面。提示&#xff1a;用户或密码错误如果验证码输入有误&#xff0c;跳转登…