一、页面展示: 二、效果图(密码:654321) 三、代码: html文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>模拟验证码输入01</title>
<link rel="stylesheet" href="index.css">
</head>
<body>
<main class="code">
<section class="show">
<p class="tit">请输入6位验证码</p>
<p class="content"></p>
</section>
<section class="input">
<ul class="keyboard">
<li class="item">1</li>
<li class="item">2</li>
<li class="item">3</li>
<li class="item">4</li>
<li class="item">5</li>
<li class="item">6</li>
<li class="item">7</li>
<li class="item">8</li>
<li class="item">9</li>
<li class="item">0</li>
</ul>
</section>
</main>
<p class="tip">霹雳一闪 六连??????</p>
</body>
<script src="index.js"></script>
</html>
index.css
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
li {
list-style: none;
}
body, html {
height: 100%;
}
body {
font-family: PingFang SC,Microsoft YaHei,Helvetica Neue,Helvetica,Hiragino Sans GB,Arial,sans-serif;
background: #000 url('flash.gif') center 80px no-repeat;
background-size: 400px;
}
.code {
position: relative;
z-index: 10;
margin: 60px auto 0;
width: 400px;
border: 1px solid #666;
border-radius: 10px;
background-color: #fff;
overflow: hidden;
}
.tip {
position: absolute;
z-index: 1;
top: 320px;
font-size: 20px;
color: #fff;
text-align: center;
width: 100%;
}
.show {
padding-top: 20px;
}
.show>.tit {
text-align: center;
font-size: 20px;
font-weight: bold;
color: #333;
}
.show>.list {
margin: 20px auto 0;
display: flex;
justify-content: center;
}
.show>.content {
margin: 20px auto 0;
text-align: center;
color: skyblue;
font-size: 30px;
letter-spacing: 5px;
height: 40px;
width: 150px;
border-bottom: 1px solid #333;
}
.input {
margin-top: 60px;
padding: 10px 5px;
background-color: #eee;
border-top: 1px solid #333;
}
.input>.tit {
font-size: 12px;
color: #333;
text-align: center;
}
.keyboard {
display: flex;
justify-content: space-around;
flex-wrap: wrap;
}
.keyboard>.item {
cursor: pointer;
margin-top: 10px;
width: 120px;
height: 30px;
line-height: 30px;
background-color: #fff;
border: 1px solid #666;
border-radius: 5px;
text-align: center;
color: #333;
font-size: 16px;
font-weight: bold;
}
index.js
var main = document.querySelector('.code')
var content = document.querySelector('.show>.content')
var listItems = document.querySelectorAll('.list>.item')
var keyItems = document.querySelectorAll('.keyboard>.item')
var keyItemsLen = keyItems.length
var codeIdx = 0;
for (let idx = 0; idx < keyItemsLen; idx++) {
keyItems[idx].addEventListener('click', function() {
if (codeIdx > 5) {
return
}
content.innerText += this.innerText.toString()
codeIdx++
if (content.innerText === '654321') {
main.style.display = 'none';
}
})
}
|