5
5
< meta name ="viewport " content ="width=device-width, initial-scale=1.0 ">
6
6
< meta http-equiv ="X-UA-Compatible " content ="ie=edge ">
7
7
{{style}}
8
+ < style >
9
+ main input [type = checkbox ] {
10
+ opacity : 1 ;
11
+ width : auto;
12
+ line-height : 1 ;
13
+ height : 1.5em ;
14
+ }
15
+ .progress {
16
+ position : fixed;
17
+ top : 80px ;
18
+ right : 40px ;
19
+ border-left : 5px solid # ddd ;
20
+ padding-left : 15px ;
21
+ font-size : 18px ;
22
+ color : # 666 ;
23
+ }
24
+
25
+ .progress span {
26
+ color : # 3bc536 ;
27
+ }
28
+ </ style >
8
29
< title > Frontend Tech List: an overall frontend-tech list for developers</ title >
9
30
</ head >
10
31
< body >
11
- < main style ="margin: 50px auto; max-width: 800px; ">
32
+ < div class ="progress "> 当前已完成阅读量:< span id ="js-progress "> 10%</ span > </ div >
33
+ < main id ="js-main " style ="margin: 50px auto; max-width: 800px; ">
12
34
{{md}}
13
35
</ main >
36
+ < script >
37
+ var itemKey = 'hasRead' ;
38
+ function updateProgress ( readNum , total ) {
39
+ var percentage = Math . ceil ( readNum / total * 100 ) ;
40
+ document . getElementById ( 'js-progress' ) . textContent = `${ percentage } %` ;
41
+ }
42
+
43
+ function init ( ) {
44
+ var hasRead = localStorage . getItem ( itemKey ) ;
45
+ try {
46
+ hasRead = JSON . parse ( hasRead ) || [ ] ;
47
+ }
48
+ catch ( e ) {
49
+ console . error ( e ) ;
50
+ hasRead = [ ] ;
51
+ }
52
+
53
+ var $checkboxList = document . querySelectorAll ( '[type=checkbox]' ) ;
54
+ $checkboxList . forEach ( function ( $n ) {
55
+ $n . removeAttribute ( 'disabled' ) ;
56
+ var cnt = $n . nextElementSibling . textContent ;
57
+ $n . dataset . txt = cnt ;
58
+ if ( hasRead . indexOf ( cnt ) > - 1 ) {
59
+ $n . checked = true ;
60
+ }
61
+ } ) ;
62
+
63
+ document . getElementById ( 'js-main' ) . dataset . total = $checkboxList . length ;
64
+ updateProgress ( hasRead . length , $checkboxList . length ) ;
65
+ }
66
+
67
+ init ( ) ;
68
+
69
+ document . addEventListener ( 'click' , function ( e ) {
70
+ var target = e . target ;
71
+ if ( target . tagName === 'INPUT' ) {
72
+ var txt = target . dataset . txt ;
73
+ var hasRead = localStorage . getItem ( itemKey ) ;
74
+ try {
75
+ hasRead = JSON . parse ( hasRead ) || [ ] ;
76
+ }
77
+ catch ( e ) {
78
+ console . error ( e ) ;
79
+ hasRead = [ ] ;
80
+ }
81
+ var idx = hasRead . indexOf ( txt ) ;
82
+ if ( target . checked && idx < 0 ) {
83
+ hasRead . push ( txt ) ;
84
+ }
85
+ else if ( ! target . checked && idx > - 1 ) {
86
+ hasRead . splice ( idx , 1 ) ;
87
+ }
88
+ localStorage . setItem ( itemKey , JSON . stringify ( hasRead ) ) ;
89
+
90
+ updateProgress ( hasRead . length , document . getElementById ( 'js-main' ) . dataset . total ) ;
91
+ }
92
+ } ) ;
93
+ </ script >
14
94
</ body >
15
95
</ html >
0 commit comments