@@ -4,13 +4,15 @@ var ditto = {
4
4
sidebar_id : "#sidebar" ,
5
5
edit_id : "#edit" ,
6
6
back_to_top_id : "#back_to_top" ,
7
+ theme_id : "#theme" ,
7
8
loading_id : "#loading" ,
8
9
error_id : "#error" ,
9
10
10
11
// display elements
11
12
sidebar : true ,
12
13
edit_button : true ,
13
14
back_to_top_button : true ,
15
+ theme_button : true ,
14
16
save_progress : true , // 保存阅读进度
15
17
search_bar : true ,
16
18
@@ -58,6 +60,10 @@ function initialize() {
58
60
if ( ditto . edit_button ) {
59
61
init_edit_button ( ) ;
60
62
}
63
+
64
+ if ( ditto . theme_button ) {
65
+ init_theme_button ( ) ;
66
+ }
61
67
62
68
// page router
63
69
router ( ) ;
@@ -133,12 +139,39 @@ function searchbar_listener(event) {
133
139
*/
134
140
}
135
141
142
+ function init_theme_button ( ) {
143
+ $ ( ditto . theme_id ) . show ( ) ;
144
+ // 默认主题
145
+ var currfontColor = localStorage . getItem ( 'fontColor' ) || '#0d141e' ;
146
+ var currbgColor = localStorage . getItem ( 'bgColor' ) || '#ffffff' ;
147
+ $ ( 'body' ) . css ( {
148
+ color : currfontColor ,
149
+ backgroundColor : currbgColor
150
+ } )
151
+ $ ( ditto . theme_id ) . on ( 'click' , changeTheme ) ;
152
+ }
136
153
137
154
function init_back_to_top_button ( ) {
138
155
$ ( ditto . back_to_top_id ) . show ( ) ;
139
156
$ ( ditto . back_to_top_id ) . on ( 'click' , goTop ) ;
140
157
}
141
158
159
+ // 改变主题
160
+ function changeTheme ( ) {
161
+ var fontColor = localStorage . getItem ( 'fontColor' ) || '#0d141e' ;
162
+ var bgColor = localStorage . getItem ( 'bgColor' ) || '#ffffff' ;
163
+ var fontColors = [ '#0d141e' , '#020000' , '#020702' , '#d0d3d8' ] ;
164
+ var bgColors = [ '#ffffff' , '#f6f0da' , '#c0edc6' , '#1f2022' ] ;
165
+ var currIndex = bgColors . indexOf ( bgColor ) ;
166
+ var nextIndex = ( currIndex + 1 ) >= bgColors . length ? 0 : currIndex + 1 ;
167
+ $ ( 'body' ) . css ( {
168
+ color : fontColors [ nextIndex ] ,
169
+ backgroundColor : bgColors [ nextIndex ] ,
170
+ } ) ;
171
+ localStorage . setItem ( 'fontColor' , fontColors [ nextIndex ] ) ;
172
+ localStorage . setItem ( 'bgColor' , bgColors [ nextIndex ] ) ;
173
+ }
174
+
142
175
function goTop ( e ) {
143
176
if ( e ) e . preventDefault ( ) ;
144
177
$ ( 'html, body' ) . animate ( {
0 commit comments