Skip to content

Commit dfe3373

Browse files
author
=
committed
feat: add change theme
1 parent 2613635 commit dfe3373

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

css/app.css

+26
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,32 @@ input.searchButton {
340340
cursor: pointer;
341341
}
342342

343+
#theme {
344+
display: none;
345+
position: fixed;
346+
347+
height: 17px;
348+
width: 70px;
349+
top: 70px;
350+
351+
margin-left: 930px;
352+
margin-top: 0px;
353+
354+
color: #FFF;
355+
line-height: 17px;
356+
text-align: center;
357+
font-size: 10px;
358+
359+
360+
border-radius: 5px;
361+
background-color: #AAA;
362+
}
363+
364+
#theme:hover {
365+
background-color: #444;
366+
cursor: pointer;
367+
}
368+
343369
#loading, #error {
344370
display: none;
345371
position: fixed;

index.html

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
<!--
2+
* @Descripttion:
3+
* @Author: Rick Liu
4+
* @Date: 2020-08-21 10:11:13
5+
* @LastEditors: Rick Liu
6+
* @LastEditTime: 2020-08-21 10:20:34
7+
-->
18
<!DOCTYPE html>
29
<html>
310
<head>
@@ -22,6 +29,7 @@
2229
<!-- optional -->
2330
<div id="back_to_top">back to top</div>
2431
<div id="edit">edit</div>
32+
<div id="theme">theme</div>
2533
<div id="loading">Loading ...</div>
2634
<div id="error">Oops! ... File not found!</div>
2735
<div id="flip"><div id="pageup">上一章</div><div id="pagedown">下一章</div></div>

js/ditto.js

+33
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ var ditto = {
44
sidebar_id: "#sidebar",
55
edit_id: "#edit",
66
back_to_top_id: "#back_to_top",
7+
theme_id: "#theme",
78
loading_id: "#loading",
89
error_id: "#error",
910

1011
// display elements
1112
sidebar: true,
1213
edit_button: true,
1314
back_to_top_button: true,
15+
theme_button: true,
1416
save_progress: true, // 保存阅读进度
1517
search_bar: true,
1618

@@ -58,6 +60,10 @@ function initialize() {
5860
if (ditto.edit_button) {
5961
init_edit_button();
6062
}
63+
64+
if (ditto.theme_button) {
65+
init_theme_button();
66+
}
6167

6268
// page router
6369
router();
@@ -133,12 +139,39 @@ function searchbar_listener(event) {
133139
*/
134140
}
135141

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+
}
136153

137154
function init_back_to_top_button() {
138155
$(ditto.back_to_top_id).show();
139156
$(ditto.back_to_top_id).on('click', goTop);
140157
}
141158

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+
142175
function goTop(e) {
143176
if(e) e.preventDefault();
144177
$('html, body').animate({

0 commit comments

Comments
 (0)