基于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 相关文章推荐
Javascript日期对象的dateAdd与dateDiff方法
Nov 18 Javascript
JS字符串处理实例代码
Aug 05 Javascript
js 单击式的下拉菜单效果实例
Aug 13 Javascript
js单词形式的运算符
May 06 Javascript
jQuery实现带幻灯的tab滑动切换风格菜单代码
Aug 27 Javascript
jquery.serialize() 函数语法及简单实例
Jul 08 Javascript
深入理解Commonjs规范及Node模块实现
May 17 Javascript
Vue2 监听属性改变watch的实例代码
Aug 27 Javascript
elementUI 设置input的只读或禁用的方法
Oct 30 Javascript
100行代码实现一个vue分页组功能
Nov 06 Javascript
Vue CLI3.0中使用jQuery和Bootstrap的方法
Feb 28 jQuery
微信小程序页面传多个参数跳转页面的实现方法
May 17 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中foreach循环中使用引用要注意的地方
2011/01/02 PHP
php正则取img标记中任意属性(正则替换去掉或改变图片img标记中的任意属性)
2013/08/13 PHP
PHP使用微信开发模式实现搜索已发送图文及匹配关键字回复的方法
2017/09/13 PHP
PHP 实现 JSON 数据的编码和解码操作详解
2020/04/22 PHP
jQuery学习笔记之jQuery选择器的使用
2010/12/22 Javascript
仅Firefox中链接A无法实现模拟点击以触发其默认行为
2011/07/31 Javascript
简单时间提示DEMO从0开始一直进行计时
2013/11/19 Javascript
使用forever管理nodejs应用教程
2014/06/03 NodeJs
jQuery中next方法用法实例
2015/04/24 Javascript
Javascript的表单验证-提交表单
2016/03/18 Javascript
javascript实现方法调用与方法触发小结
2016/03/26 Javascript
javascript对象的相关操作小结
2016/05/16 Javascript
值得分享的轻量级Bootstrap Table表格插件
2016/05/30 Javascript
JavaScript动态添加事件之事件委托
2016/07/12 Javascript
JavaScript编写一个贪吃蛇游戏
2017/03/09 Javascript
基于jquery实现二级联动效果
2017/03/30 jQuery
node.js+jQuery实现用户登录注册AJAX交互
2017/04/28 jQuery
图片懒加载imgLazyLoading.js使用详解
2020/09/15 Javascript
vue 集成jTopo 处理方法
2019/08/07 Javascript
python计算方程式根的方法
2015/05/07 Python
使用python实现接口的方法
2017/07/07 Python
关于python pyqt5安装失败问题的解决方法
2017/08/08 Python
Python3多线程爬虫实例讲解代码
2018/01/05 Python
python3发送邮件需要经过代理服务器的示例代码
2019/07/25 Python
解决pandas展示数据输出时列名不能对齐的问题
2019/11/18 Python
Keras Convolution1D与Convolution2D区别说明
2020/05/22 Python
PyTorch中Tensor的数据类型和运算的使用
2020/09/03 Python
Python用SSH连接到网络设备
2021/02/18 Python
HTML5适合的情人节礼物有纪念日期功能
2021/01/25 HTML / CSS
解释DataSet(ds) 和 ds as DataSet 的含义
2014/07/27 面试题
结婚保证书
2015/01/16 职场文书
简爱读书笔记
2015/06/26 职场文书
婚宴主持词
2015/06/30 职场文书
JavaScript 对象创建的3种方法
2021/11/17 Javascript
Python读写yaml文件
2022/03/20 Python
Python使用pyecharts控件绘制图表
2022/06/05 Python