@@ -93,56 +93,89 @@ function show_date_time() {
9393 }
9494 }
9595
96- <?php if ($ this ->options ->zmki_ah == '0 ' ) : ?>
97- // 跟随系统
98- const matches = matchMedia('(prefers-color-scheme: dark)').matches;
99- if (matches) {
100- document.body.classList.add('night');
101- if (cookie.get('night') !== '1') {
102- cookie.set('night', '1');
103- console.log('跟随系统开启夜间模式');
104- }
105- } else {
106- document.body.classList.remove('night');
107- if (cookie.get('night') !== '0') {
108- cookie.set('night', '0');
109- console.log('跟随系统关闭夜间模式');
110- }
111- }
112- <?php elseif ($ this ->options ->zmki_ah == '1 ' ) : ?>
113- // 自动切换
114- if (new Date().getHours() >
115- 19 ||
116- new Date().getHours() <
117- 7) {
118- document.body.classList.add('night');
119- if (cookie.get('night') !== '1') {
120- cookie.set('night', '1');
121- console.log('夜间模式自动开启');
122- }
123- } else {
124- document.body.classList.remove('night');
125- if (cookie.get('night') !== '0') {
126- cookie.set('night', '0');
127- console.log('夜间模式自动关闭');
96+ function darkHandler(darkMode, mode, write = false) {
97+ write && write && cookie.set('dark_mode', darkMode);
98+ const night = cookie.get('night');
99+ if (darkMode === '1') {
100+ // 夜间模式
101+ mode && (mode.style.backgroundImage = 'url(<?php echo THEME_URL ?> /fonts/night.svg)');
102+ if (night !== '1') {
103+ document.body.classList.add('night');
104+ cookie.set('night', '1');
105+ console.log('开启夜间模式');
106+ }
107+ } else if (darkMode === '2') {
108+ // 跟随系统
109+ mode && (mode.style.backgroundImage = 'url(<?php echo THEME_URL ?> /fonts/system.svg)');
110+ const matches = matchMedia('(prefers-color-scheme: dark)').matches;
111+ if (matches) {
112+ if (night !== '1') {
113+ document.body.classList.add('night');
114+ cookie.set('night', '1');
115+ console.log('跟随系统开启夜间模式');
116+ }
117+ } else {
118+ if (night !== '0') {
119+ document.body.classList.remove('night');
120+ cookie.set('night', '0');
121+ console.log('跟随系统关闭夜间模式');
122+ }
123+ }
124+ } else if (darkMode === '3') {
125+ // 自动切换
126+ mode && (mode.style.backgroundImage = 'url(<?php echo THEME_URL ?> /fonts/auto.svg)');
127+ const hours = new Date().getHours();
128+ if (hours > 19 || hours < 7) {
129+ if (night !== '1') {
130+ document.body.classList.add('night');
131+ cookie.set('night', '1');
132+ console.log('夜间模式自动开启');
133+ }
134+ } else {
135+ if (night !== '0') {
136+ document.body.classList.remove('night');
137+ cookie.set('night', '0');
138+ console.log('夜间模式自动关闭');
139+ }
140+ }
141+ } else {
142+ // 日间模式
143+ mode && (mode.style.backgroundImage = 'url(<?php echo THEME_URL ?> /fonts/sun.svg)');
144+ if (night !== '0') {
145+ document.body.classList.remove('night');
146+ cookie.set('night', '0');
147+ console.log('开启日间模式');
148+ }
128149 }
129150 }
130- <?php else : ?>
131- // 手动切换夜间模式
151+
152+ // 暗黑模式
153+ const dark = cookie.get('dark_mode');
154+ // 模式开关
132155 const modeSwitch = document.querySelector('.my_mode_switch');
156+ darkHandler(dark, modeSwitch);
157+ const darkList = document.querySelector('.dark_mode_list');
158+
133159 modeSwitch && modeSwitch.addEventListener('click', e => {
134160 e.stopPropagation();
135- if (cookie.get('night') === '1') {
136- document.body.classList.remove('night');
137- cookie.set('night', '0');
138- console.log('关闭夜间模式');
139- } else {
140- document.body.classList.add('night');
141- cookie.set('night', '1');
142- console.log('开启夜间模式');
161+ if (darkList) {
162+ if (darkList.classList.contains('open')) {
163+ darkList.classList.remove('open');
164+ } else {
165+ darkList.classList.add('open');
166+ }
143167 }
144168 });
145- <?php endif ; ?>
169+
170+ const modeSelectors = ['.dark_mode_sun', '.dark_mode_night', '.dark_mode_system', '.dark_mode_auto']
171+ modeSelectors.forEach((selector, index) => {
172+ const ele = document.querySelector(selector);
173+ ele && ele.addEventListener('click', e => {
174+ e.stopPropagation();
175+ darkHandler(String(index), modeSwitch, true);
176+ darkList && darkList.classList.remove('open');
177+ })
178+ })
146179 })();
147180</script>
148181
@@ -160,12 +193,53 @@ function show_date_time() {
160193 <?php endif ; ?>
161194
162195 .my_mode_switch {
163- background-image: url("<?php echo THEME_URL ?> /fonts/night.svg");
196+ background-image: url("<?php echo THEME_URL ?> /fonts/sun.svg");
197+ }
198+
199+ body.night .dropdown-menu {
200+ background-color: #221e1e;
201+ border: 1px solid #232b32;
202+ }
203+
204+ .dark_mode_list ul {
205+ margin-top: 40px;
206+ min-width: 80px;
164207 }
165- body.night .my_mode_switch {
208+
209+ .dark_mode_list .dark_mode_li {
210+ height: 24px;
211+ padding: 0 10px;
212+ }
213+
214+ .dark_mode_list .dark_mode_li span {
215+ display: inline-flex;
216+ cursor: pointer;
217+ }
218+
219+ .dark_mode_list i {
220+ display: inline-block;
221+ width: 16px;
222+ height: 16px;
223+ background-size: 16px 16px;
224+ margin-right: 5px;
225+ }
226+
227+ .dark_mode_sun i {
166228 background-image: url("<?php echo THEME_URL ?> /fonts/sun.svg");
167229 }
168230
231+ .dark_mode_night i {
232+ background-image: url("<?php echo THEME_URL ?> /fonts/night.svg");
233+ }
234+
235+ .dark_mode_system i {
236+ background-image: url("<?php echo THEME_URL ?> /fonts/system.svg");
237+ }
238+
239+ .dark_mode_auto i {
240+ background-image: url("<?php echo THEME_URL ?> /fonts/auto.svg");
241+ }
242+
169243 span.title, .main-menu {
170244 font-size: <?php $ this ->options ->menu_title (); ?> px;
171245 }
0 commit comments