自定义input

利用弹性过渡自定义一个带 tip 提示框的 input 输入框

效果图

html

1
2
3
4
5
6
7
8
9
10
11
12
13
<main class="main">
<label>
<input
type="text"
required
id="username"
autocomplete="off"
placeholder="请输入内容"
pattern="^\w+$"
/>
<span class="poptip">仅支持字母, 数字和下划线组合!!</span>
</label>
</main>

css

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
main {
width: 100%;
height: 229px;
display: flex;
}
label {
margin: auto;
}
input {
display: block;
width: 229px;
padding: 0.8em;
outline: none;
border: 1px solid #e3e3e3;
border-radius: 2px;
}
input:focus,
input:hover {
border-color: #b4a078;
}
input:not(:placeholder-shown) {
border-color: #be4141;
box-shadow: 0 0 0 2px rgba(255, 100, 97, 0.2);
}
input:not(:placeholder-shown) + .poptip {
color: #be4141;
}
input:valid {
border-color: #b4a078;
box-shadow: 0 0 0 2px rgba(180, 160, 120, 0.2);
}
input:valid + .poptip {
color: unset;
}
input:not(:focus) + .poptip {
transform: scale(0);
animation: elastic-dec 0.15s;
}
@keyframes elastic-dec {
from {
transform: scale(1);
}
to {
transform: scale(0);
animation-timing-function: cubic-bezier(0.25, 0.1, 0.25, 0.1);
}
}
input:focus + .poptip {
transform: scale(1);
animation: elastic-grow 0.25s;
}
@keyframes elastic-grow {
from {
transform: scale(0);
}
70% {
transform: scale(1);
animation-timing-function: cubic-bezier(0.1, 0.25, 0.1, 0.25);
}
}
.poptip {
display: inline-block;
width: 236px;
font-size: 13px;
padding: 0.6em;
background: #fafafa;
position: relative;
margin-left: -3px;
margin-top: 6px;
border-radius: 2px;
filter: drop-shadow(0 0 1px rgba(0, 0, 0, 0.23456));
transform-origin: 15px -6px;
}
.poptip::before {
content: "";
position: absolute;
top: -6px;
left: 10px;
border: 9px solid transparent;
border-bottom-color: #fafafa;
border-top-width: 0;
}