这是一种网页上比较常用的效果,当点击获取手机验证码之后,会有一个倒计时的效果,一般是30秒开始到0就变为“获取验证码”,下面动画图片示例:
代码中的样式是基于 jQuery WeUI 组件,针对于微信公众号开发前端组件!
引入文件
- <link href="https://cdn.bootcss.com/weui/1.1.2/style/weui.min.css" rel="stylesheet">
- <link href="https://cdn.bootcss.com/jquery-weui/1.2.0/css/jquery-weui.min.css" rel="stylesheet">
- <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
- <script src="https://cdn.bootcss.com/jquery-weui/1.2.0/js/jquery-weui.min.js"></script>
HTML代码
- <div class="weui-form-data">
- <div class="weui-cells weui-cells_form">
- <div class="weui-cell weui-cell_vcode">
- <div class="weui-cell__hd">
- <label class="weui-label">手机号码</label>
- </div>
- <div class="weui-cell__bd">
- <input id="tel" class="weui-input" type="tel" placeholder="请输入手机号码">
- </div>
- <div class="weui-cell__ft">
- <input id="btnSendCode" class="weui-vcode-btn weui-input-btn" type="button" value="获取验证码" onclick="sendCodeBtn()">
- </div>
- </div>
- <div class="weui-cell weui-cell_vcode">
- <div class="weui-cell__hd">
- <label class="weui-label">验证码</label>
- </div>
- <div class="weui-cell__bd">
- <input id="code" class="weui-input" type="number" placeholder="请输入验证码">
- </div>
- <div class="weui-cell__ft">
- <button class="weui-vcode-btn weui-hide-btn"></button>
- </div>
- </div>
- </div>
- <div class="weui-btn-area weui-btn-martop">
- <a class="weui-btn weui-btn_primary" href="javascript:" onclick="authBtn()">验证</a>
- </div>
- </div>
jQuery代码
- <script>
- var InterValObj; //timer变量,控制时间
- var count = 30; //间隔函数,1秒执行
- var curCount; //当前剩余秒数
- var tel;
- var code;
- // 点击获取验证码按钮
- function sendCodeBtn(){
- tel = $('#tel').val();
- if(!tel || !/^((13|14|15|17|18)[0-9]{1}\d{8})$/.test(tel)){
- $.toptip('请输入正确的手机号');
- }else{
- sendMessage();
- }
- }
- // 获取验证码
- function sendMessage(){
- curCount = count;
- tel = $('#tel').val();
- $("#btnSendCode").attr("disabled", "true");
- $("#btnSendCode").val("重新获取(" + curCount + "s)");
- InterValObj = window.setInterval(SetRemainTime, 1000);
- //请求后台获取验证码
- $.toast('获取验证码成功');
- }
- //倒计时处理
- function SetRemainTime(){
- if (curCount == 0) {
- window.clearInterval(InterValObj);
- $("#btnSendCode").removeAttr("disabled");
- $("#btnSendCode").val("重新获取");
- }
- else {
- curCount--;
- $("#btnSendCode").val("重新获取(" + curCount + "s)");
- }
- }
- // 点击验证码校验按钮
- function authBtn(){
- tel = $('#tel').val();
- code = $('#code').val();
- if(!tel || !/^((13|14|15|17|18)[0-9]{1}\d{8})$/.test(tel)){
- $.toptip('请输入正确的手机号');
- }
- else if(!code || !/\d{6}/.test(code)){
- $.toptip('请输入六位手机验证码');
- }
- else{
- //请求后台校验验证码
- $.toast('验证成功');
- }
- }
- </script>
在线演示地址:
博主只是一名前端的小白,只是把自己用到的知识分享一下,要是有什么不对的地方,欢迎大家提出~~
继续阅读
评论