2020-04-02 20:37:34 +08:00
|
|
|
/**
|
|
|
|
* 日间/夜间 模式切换
|
|
|
|
*/
|
2020-04-02 22:07:55 +08:00
|
|
|
const modeToggleBtn = document.querySelector('.mode-toggle');
|
2020-04-02 20:37:34 +08:00
|
|
|
const iconDom = modeToggleBtn.querySelector('i');
|
2020-04-03 18:18:42 +08:00
|
|
|
// const articleContent = document.querySelector('.article-content');
|
|
|
|
const articleContent = document.querySelector('.main-content');
|
2020-04-02 20:37:34 +08:00
|
|
|
const modeConfig = JSON.parse(localStorage.getItem('ils_x'));
|
|
|
|
if (modeConfig) {
|
|
|
|
|
|
|
|
if (modeConfig.prefersColorScheme === 'dark') {
|
|
|
|
|
|
|
|
if (modeConfig.isDark) {
|
|
|
|
document.body.classList.remove('light-mode');
|
2020-04-03 18:18:42 +08:00
|
|
|
articleContent.classList.remove('night-code-theme');
|
2020-04-02 20:37:34 +08:00
|
|
|
iconDom.className = 'fa fa-lightbulb-o';
|
|
|
|
} else {
|
|
|
|
iconDom.className = 'fa fa-moon-o';
|
|
|
|
document.body.classList.add('light-mode');
|
2020-04-03 18:18:42 +08:00
|
|
|
articleContent.classList.add('night-code-theme');
|
2020-04-02 20:37:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if (modeConfig.isDark) {
|
|
|
|
document.body.classList.remove('dark-mode');
|
2020-04-03 18:18:42 +08:00
|
|
|
articleContent.classList.remove('night-code-theme');
|
2020-04-02 20:37:34 +08:00
|
|
|
iconDom.className = 'fa fa-moon-o';
|
|
|
|
} else {
|
|
|
|
document.body.classList.add('dark-mode');
|
|
|
|
iconDom.className = 'fa fa-lightbulb-o';
|
2020-04-03 18:18:42 +08:00
|
|
|
articleContent.classList.add('night-code-theme');
|
2020-04-02 20:37:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const setItemUtil = (modeClass, prefersColorScheme) => {
|
|
|
|
document.body.classList.toggle(modeClass);
|
|
|
|
const isDark = document.body.className.indexOf(modeClass) === -1;
|
2020-04-03 18:18:42 +08:00
|
|
|
|
|
|
|
if (isDark) {
|
|
|
|
iconDom.className = 'fa fa-moon-o';
|
|
|
|
articleContent.classList.remove('night-code-theme');
|
|
|
|
} else {
|
|
|
|
iconDom.className = 'fa fa-lightbulb-o';
|
|
|
|
articleContent.classList.add('night-code-theme');
|
|
|
|
}
|
2020-04-02 20:37:34 +08:00
|
|
|
localStorage.setItem('ils_x', JSON.stringify(
|
|
|
|
{
|
|
|
|
prefersColorScheme: prefersColorScheme,
|
|
|
|
isDark: isDark
|
|
|
|
}
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
modeToggleBtn.addEventListener('click', function (e) {
|
|
|
|
|
|
|
|
if (
|
|
|
|
window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches
|
|
|
|
) {
|
|
|
|
setItemUtil('light-mode', 'dark');
|
|
|
|
} else {
|
|
|
|
setItemUtil('dark-mode', 'light');
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|