<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0">
<title> </title>
<script type="text/javascript" src="http://www.daixiaorui.com/Public/js/jquery.min.js"></script>
<style>
@keyframes animate {
100% {
opacity: 0;
}
}
.container{
padding: 5%;
}
.vcode{
position: relative;
width: 100%;
overflow: hidden;
}
.code{
width: 100%;
padding: 0;
height: 0px;
font-size: 35px;
overflow: hidden;
border: none;
outline: none;
opacity: 0;
margin-left: -100%; /* ios上透明度为0时依然有光标 */
-webkit-tap-highlight-color: transparent;/*ios手机上input和lable都会出现点击有灰色背景闪动*/
}
/* 第一种样式 下划线 */
.labels{
display: flex;
display: -webkit-flex;
width: 100%;
height: 40px;
justify-content: space-between;
-webkit-justify-content: space-between;
margin-bottom: 40px;
-webkit-tap-highlight-color: transparent; /*防止ios手机上input和lable都会出现点击有灰色背景闪动*/
}
.label{
height: 34px;
line-height:34px;
width: 12%;
border-bottom: solid 2px #313131;
float: left;
color: #313131;
font-size: 20px;
text-align: center;
padding-bottom: 4px;
}
.labels2 .label{
height: 34px; line-height:34px;
width: 12%;
border: solid 1px #746f6f;
float: left;
color: #313131;
font-size: 20px;
text-align: center;
padding : 4px;
}
.active:after{
content: ‘ ‘;
display: inline-block;
height: 100%;
width: 2px;
height:34px;
background: #ff8000;
animation: .8s animate linear infinite;
}
</style>
</head>
<body>
<div class="container">
<h2>输入验证码:</h2>
<div class="vcode" id=‘vertifycode‘>
<input type="text" maxlength=‘6‘ autofocus 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 class="labels labels2" >
<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>
</body>
<script type="text/javascript">
$(function(){
// i 为label的索引下标
var i = 0;
$("#code").focus(function(){
if($(".labels .active").length==0){
$(".labels label:nth-child(1)").addClass("active");
}
})
$(‘input‘).keyup(function (event) {
var reg = /[A-Za-z0-9]/;
if(reg.test(event.key)){
$(".labels label").removeClass("active");
if(i<$(".labels label").length) {
//46删除键,8回退键
if(event.keyCode==46 || event.keyCode==8) {
i--;
i = i<0?0:i;
$("label:nth-child("+(i+1)+")").html("").addClass("active")
}else {
$("label:nth-child("+(i+1)+")").html(event.key)
$("label:nth-child("+(i+2)+")").addClass("active")
i++;
}
}else{
if(event.keyCode==46 || event.keyCode==8) {
i=$(".labels label").length-1;
$("label:nth-child("+(i+1)+")").html("").addClass("active")
}
}
}
})
});
</script>
</html>
-----参考原文:https://blog.csdn.net/dongguan_123/article/details/79961216------