HTML+CSS+Javascript 模拟验证码输入

一、页面展示:
在这里插入图片描述
二、效果图(密码: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">霹雳一闪&nbsp;六连⚡⚡⚡⚡⚡⚡</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) { // 输入超过6位时,停止执行
      return
    }
    // 对应位置写入数字
    content.innerText += this.innerText.toString()
    codeIdx++

    if (content.innerText === '654321') {
      main.style.display = 'none';
    }
  })
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,以下是一个简单的示例代码: HTML代码: ```html <form> <label for="phone">手机号:</label> <input type="tel" id="phone" name="phone" placeholder="请输入手机号"> <button type="button" id="send-code">发送验证码</button> </form> ``` CSS代码: ```css input[type="tel"] { width: 200px; padding: 5px; margin-right: 10px; } button { padding: 5px 10px; background-color: #0099ff; color: #fff; border: none; border-radius: 3px; cursor: pointer; } button:disabled { background-color: #ccc; cursor: not-allowed; } ``` JavaScript代码: ```javascript // 获取DOM元素 const phoneInput = document.getElementById('phone'); const sendCodeBtn = document.getElementById('send-code'); // 发送验证码函数 function sendCode() { // 获取手机号 const phone = phoneInput.value.trim(); // 判断手机号是否为空 if (!phone) { alert('请输入手机号'); return; } // TODO: 发送验证码请求 console.log(`向 ${phone} 发送验证码...`); // 模拟发送验证码的过程 sendCodeBtn.disabled = true; setTimeout(() => { sendCodeBtn.disabled = false; }, 5000); } // 绑定发送验证码按钮的点击事件 sendCodeBtn.addEventListener('click', sendCode); ``` 以上代码实现了一个简单的发送验证码功能,点击发送验证码按钮会在控制台输出向对应手机号发送验证码的信息,并且在按钮点击后 5 秒内禁用按钮,模拟了发送验证码的过程。你可以在 TODO 部分实现具体的发送验证码请求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jus sing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值