11<!DOCTYPE html>
22< html lang ="en ">
3+ < head >
4+ < meta charset ="UTF-8 "/>
5+ < meta http-equiv ="X-UA-Compatible "content ="IE=edge "/>
6+ < meta name ="viewport "content ="width=device-width, initial-scale=1.0 "/>
7+ < title > Document</ title >
8+ < style >
9+ * {
10+ box-sizing : border-box;
11+ }
312
4- < head >
5- < meta charset ="UTF-8 ">
6- < meta http-equiv ="X-UA-Compatible "content ="IE=edge ">
7- < meta name ="viewport "content ="width=device-width, initial-scale=1.0 ">
8- < title > Document</ title >
9- < style >
10- * {
11- box-sizing : border-box;
12- }
13-
14- body {
15- background : # eee ;
16- font-family : "Roboto" , sans-serif;
17- }
13+ body {
14+ background : # eee ;
15+ font-family : 'Roboto' , sans-serif;
16+ }
1817
19- .form-item {
20- position : relative;
21- margin : 45px 0 ;
22- display : flex;
23- flex-direction : column;
24- align-items : center;
25- }
18+ .form-item {
19+ position : relative;
20+ margin : 45px 0 ;
21+ display : flex;
22+ flex-direction : column;
23+ align-items : center;
24+ }
2625
27- .form-item input {
28- width : 100% ;
29- height : 40px ;
30- /* 去掉四周的边框 */
31- border : none;
32- outline : none;
33- font-size : 18px ;
34- color : # 666 ;
35- }
26+ .form-item input {
27+ width : 100% ;
28+ height : 40px ;
29+ /* 去掉四周的边框 */
30+ border : none;
31+ outline : none;
32+ font-size : 18px ;
33+ color : # 666 ;
34+ }
3635
37- /* 给 label 绝对定位 */
38- .form-item label {
39- position : absolute;
40- top : 50% ;
41- transform : translate (0 , -50% );
42- left : 10px ;
43- font-size : 18px ;
44- color : # 999 ;
45- transition : all0.3s ease-in-out;
46- cursor : text;
47- }
36+ /* 给 label 绝对定位 */
37+ .form-item label {
38+ position : absolute;
39+ top : 50% ;
40+ transform : translate (0 , -50% );
41+ left : 10px ;
42+ font-size : 18px ;
43+ color : # 999 ;
44+ transition : all0.3s ease-in-out;
45+ cursor : text;
46+ }
4847
49- .form-item .bar {
50- width : 100% ;
51- height : 2px ;
52- background : # ffffff ;
53- transition : all0.3s ease-in-out;
54- }
48+ .form-item .bar {
49+ width : 100% ;
50+ height : 2px ;
51+ background : # ffffff ;
52+ transition : all0.3s ease-in-out;
53+ }
5554
56- .form-item input : focus ~ .bar {
57- background-color : black;
58- animation : show0.3s ease-in-out forwards;
59- }
55+ .form-item input : focus ~ .bar {
56+ background-color : black;
57+ animation : show0.3s ease-in-out forwards;
58+ }
6059
61- .form-item input : focus ~ label {
62- top : -50% ;
63- left : 0 ;
64- }
60+ .form-item input : focus ~ label {
61+ top : -50% ;
62+ left : 0 ;
63+ }
6564
66- @keyframes show {
67- 0 % {
68- width : 0 ;
65+ . form-item input . has-value ~ label {
66+ top : -50 % ;
67+ left : 0 ;
6968}
7069
71- 100% {
72- width : 100% ;
70+ @keyframes show {
71+ 0% {
72+ width : 0 ;
73+ }
74+
75+ 100% {
76+ width : 100% ;
77+ }
7378}
74- }
75- </ style >
76- </ head >
79+ </ style >
80+ </ head >
81+
82+ < body >
83+ < div class ="form-item ">
84+ < input type ="text "id ="username "/>
85+ < div class ="bar "> </ div >
86+ < label for ="username "> User Name</ label >
87+ </ div >
88+ < script >
89+ const input = document . getElementById ( 'username' )
7790
78- < body >
79- < div class ="form-item ">
80- < input type ="text "id ="username ">
81- < div class ="bar "> </ div >
82- < label for ="username "> User Name</ label >
83- </ div >
84- </ body >
91+ input . addEventListener ( 'focus' , ( ) => {
92+ input . classList . remove ( 'has-value' )
93+ } )
8594
86- </ html >
95+ input . addEventListener ( 'blur' , ( ) => {
96+ if ( input . value . trim ( ) !== '' ) {
97+ input . classList . add ( 'has-value' )
98+ }
99+ } )
100+ </ script >
101+ </ body >
102+ </ html >