基于JS实现类似支付宝支付密码输入框


Posted in Javascript onSeptember 02, 2016

本文实现的是一个类似支付宝支付密码的界面,只可以输入数字,且只可以输入6位

首先给大家展示下效果图,如果感觉不错,请参考实现代码。

基于JS实现类似支付宝支付密码输入框

1、样式表

.wrap{
margin: 10px auto;
width: 329px;
height: 640px; 
padding-top: 200px;
}
.inputBoxContainer{
width: 240px;
height: 50px;
margin: 0 auto;
position: relative;
}
.inputBoxContainer .bogusInput{
width: 100%;
height: 100%;
border: #c3c3c3 1px solid;
border-radius: 7px;
-moz-border-radius: 7px;
-webkit-border-radius: 7px;
overflow: hidden;
position: absolute;
z-index: 0;
}
.inputBoxContainer .realInput{
width: 100%;
height: 100%;
position: absolute;
top:0;
left: 0;
z-index: 1;
filter:alpha(opacity=0);
-moz-opacity:0;
opacity:0;
}
.inputBoxContainer .bogusInput input{
padding: 0;
width: 16.3%;
height: 100%;
float:left;
background: #ffffff;
text-align: center;
font-size: 20px;
border: none;
border-right: #C3C3C3 1px solid;
}
.inputBoxContainer .bogusInput input:last-child{
border: none;
}
.confirmButton{
width: 240px;
height: 45px;
border-radius: 7px;
-moz-border-radius: 7px;
-webkit-border-radius: 7px;
background: #f4f4f4;
border: #d5d5d5 1px solid;
display: block;
font-size: 16px;
margin: 30px auto;
margin-bottom: 20px;
}
.showValue{
width: 240px;
height: 22px;
line-height: 22px;
font-size: 16px;
text-align: center;
margin: 0 auto;
}

2、HTML代码

<div class="wrap">
<div class="inputBoxContainer" id="inputBoxContainer">
<input type="text" class="realInput"/>
<div class="bogusInput">
<input type="password" maxlength="6" disabled/>
<input type="password" maxlength="6" disabled/>
<input type="password" maxlength="6" disabled/>
<input type="password" maxlength="6" disabled/>
<input type="password" maxlength="6" disabled/>
<input type="password" maxlength="6" disabled/>
</div>
</div>
<button id="confirmButton" class="confirmButton">查看</button>
<p class="showValue" id="showValue"></p>
</div>

3、js代码控制逻辑效果

(function(){
var container = document.getElementById("inputBoxContainer");
boxInput = {
maxLength:"",
realInput:"",
bogusInput:"",
bogusInputArr:"",
callback:"",
init:function(fun){
var that = this;
this.callback = fun;
that.realInput = container.children[0];
that.bogusInput = container.children[1];
that.bogusInputArr = that.bogusInput.children;
that.maxLength = that.bogusInputArr[0].getAttribute("maxlength");
that.realInput.oninput = function(){
that.setValue();
}
that.realInput.onpropertychange = function(){
that.setValue();
}
},
setValue:function(){
this.realInput.value = this.realInput.value.replace(/\D/g,"");
console.log(this.realInput.value.replace(/\D/g,""))
var real_str = this.realInput.value;
for(var i = 0 ; i < this.maxLength ; i++){
this.bogusInputArr[i].value = real_str[i]?real_str[i]:"";
}
if(real_str.length >= this.maxLength){
this.realInput.value = real_str.substring(0,6);
this.callback();
}
},
getBoxInputValue:function(){
var realValue = "";
for(var i in this.bogusInputArr){
if(!this.bogusInputArr[i].value){
break;
}
realValue += this.bogusInputArr[i].value;
}
return realValue;
}
}
})()
boxInput.init(function(){
getValue();
});
document.getElementById("confirmButton").onclick = function(){
getValue();
}
function getValue(){
document.getElementById("showValue").innerText = boxInput.getBoxInputValue();
}

以上所述是小编给大家介绍的基于JS实现类似支付宝支付密码输入框,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!

Javascript 相关文章推荐
兼容ie、firefox的图片自动缩放的css跟js代码分享
Jan 21 Javascript
jQuery实现分章节锚点“回到顶部”动画特效代码
Oct 23 Javascript
bootstrap输入框组代码分享
Jun 07 Javascript
CSS3 3D 技术手把手教你玩转
Sep 02 Javascript
AngularJS 2.0入门权威指南
Oct 08 Javascript
bootstrap模态框消失问题的解决方法
Dec 02 Javascript
BOM之navigator对象和用户代理检测
Feb 10 Javascript
JS实现点击链接切换显示隐藏内容的方法
Oct 19 Javascript
jQuery.parseJSON()函数详解
Feb 28 jQuery
JavaScript面向对象中接口实现方法详解
Jul 24 Javascript
浏览器JavaScript调试功能无法使用解决方案
Sep 18 Javascript
javascript中导出与导入实现模块化管理教程
Dec 03 Javascript
JavaScript中Number对象的toFixed() 方法详解
Sep 02 #Javascript
Node.js实现兼容IE789的文件上传进度条
Sep 02 #Javascript
AngularJs  Understanding Angular Templates
Sep 02 #Javascript
jquery 中toggle的2种用法详解(推荐)
Sep 02 #Javascript
浅谈JS中的三种字符串连接方式及其性能比较
Sep 02 #Javascript
AngularJs  E2E Testing 详解
Sep 02 #Javascript
解决node.js安装包失败的几种方法
Sep 02 #Javascript
You might like
PHP开发中常用的字符串操作函数
2011/02/08 PHP
ThinkPHP写第一个模块应用
2012/02/20 PHP
详解PHP匿名函数与注意事项
2016/03/29 PHP
phpstorm 配置xdebug的示例代码
2019/03/31 PHP
PHP7创建销毁session的实例方法
2020/02/03 PHP
CL vs ForZe BO5 第二场 2.13
2021/03/10 DOTA
javascript打开新窗口同时关闭旧窗口
2009/01/16 Javascript
ExtJS 2.0实用简明教程之应用ExtJS
2009/04/29 Javascript
基于jQuery的message插件实现右下角弹出消息框
2011/01/11 Javascript
js实现两点之间画线的方法
2015/05/12 Javascript
在HTML中插入JavaScript代码的示例
2015/06/03 Javascript
JS+CSS实现表格高亮的方法
2015/08/05 Javascript
JS实现滑动菜单效果代码(包括Tab,选项卡,横向等效果)
2015/09/24 Javascript
JavaScript操作class和style样式代码详解
2016/02/13 Javascript
微信和qq时间格式模板实例详解
2016/10/21 Javascript
layui文件上传实现代码
2017/05/20 Javascript
原生JS实现不断变化的标签
2017/05/22 Javascript
详解VUE2.X过滤器的使用方法
2018/01/11 Javascript
详解Vue开发微信H5微信分享签名失败问题解决方案
2018/08/09 Javascript
video.js 一个页面同时播放多个视频的实例代码
2018/11/27 Javascript
使用Vue.set()方法实现响应式修改数组数据步骤
2019/11/09 Javascript
详解Python实现多进程异步事件驱动引擎
2017/08/25 Python
python实现对求解最长回文子串的动态规划算法
2018/06/02 Python
Python中一般处理中文的几种方法
2019/03/06 Python
Python通过TensorFlow卷积神经网络实现猫狗识别
2019/03/14 Python
使用OpenCV实现人脸图像卡通化的示例代码
2021/01/15 Python
详解FireFox下Canvas使用图像合成绘制SVG的Bug
2019/07/10 HTML / CSS
简洁自适应404页面HTML好看的404源码
2020/12/16 HTML / CSS
新学期家长寄语
2014/01/19 职场文书
运动会铅球比赛加油稿
2014/09/26 职场文书
银行委托书范本
2014/09/28 职场文书
2015年小学一年级班主任工作总结
2015/05/21 职场文书
男生贾里读书笔记
2015/06/30 职场文书
二婚主持词
2015/06/30 职场文书
学术研讨会主持词
2015/07/04 职场文书
MySQL数据库 安全管理
2022/05/06 MySQL