优化默认密码输入框

password
主要是为了弥补在移动端原生密码输入框默认样式比较单调,通过这样能够实现自定义密码框输出样式。
代码如下,仅供参考—

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<title>优化默认密码框</title>
<meta name="viewport" content="width=device-width,initial-scale=1.0,user-scaleable=0" />
<style>
* {
margin: 0;
padding: 0;
}

html {
height: 100%;
font-size: 16px;
}

body {
height: 100%;
background-color: skyblue;
}

.container {
position: relative;
margin: 100px 100px;
}

.password {
display: block;
}

ul {
display: inline-block;
border: 1px solid #fff;
margin: 0 auto;
font-size: 0;
position: relative;
width: 12rem;
height: 2rem;
left: 50%;
transform: translateX(-50%);
}

li {
display: inline-block;
width: 2rem;
height: 2rem;
background-color: #fff;
font-size: 1.2rem;
font-weight: 700;
text-align: center;
line-height: 2rem;
border-left: 1px solid #e6e6e6;
box-sizing: border-box;
vertical-align: middle;
overflow: hidden;
}

li:first-child {
border-left: 0 none
}

.input {
position: absolute;
top: 0;
left: -100%;
width: 200%;
height: 100%;
opacity: 0;
text-indent: -9999px;
}
</style>
</head>

<body>
<div class="container">
<div class="password">
<ul>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</div>
<input class="input" type="tel" maxlength="6" id="input">
<label for="input"></label>
</div>
<script src="./zepto.js"></script>
<script>
$('body').on('input', '.input', function() {
var $this = $(this),
$inputs = $this.parent().find('.password li'),
password = $this.val() + '';
var numLen = 6;
for (var i = 0; i < numLen; i++) {
if (password[i]) {
$($inputs[i]).text('●');
} else {
$($inputs[i]).text('');
}
}
if (password.length == 6) {
alert('6位密码已输完');
$this.val('');
$inputs.html('');
}
});
</script>
</body>

</html>

点击查看效果