icarus主题默认不支持黑暗模式,相关问题,参考这个文章,使用hexo5的注入器功能添加支持,效果见主页
添加icon图标
_config.icarus.yml1 2 3 4 5 6 7 8
| navbar: links: 切换亮色: icon: fa-solid fa-circle-half-stroke url: javascript:;
|
重启 npm run server
就可以看到按钮
添加按钮事件
在scripts
下添加一个js文件比如:
scripts/injector.js
scripts/injector.js1 2 3 4
| hexo.extend.injector.register('body_end', `<script src="/article/js/darkmode.js"></script>`, 'default'); hexo.extend.injector.register('head_begin', `<link rel="stylesheet" href="/article/css/darkmode.css"></link>`, 'default');
|
在主题配置文件中添加js和css文件
themes/icarus/source/js/darkmode.js1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| (function(){ var navbarEnd = document.getElementsByClassName('navbar-end'); if (navbarEnd.length === 0) { return; } var navbarMenu = navbarEnd[0].firstElementChild; if (navbarMenu === null) { return; } navbarMenu.removeAttribute('target'); navbarMenu.removeAttribute('href'); navbarMenu.setAttribute('onclick', 'darkmode()');
var darkmode = localStorage.getItem('darkmode'); if (darkmode === null) { darkmode = false; localStorage.setItem('darkmode', darkmode); return; } if (darkmode === 'true') { document.body.classList.add('darkmode'); } })();
function darkmode() { var darkmode = localStorage.getItem('darkmode'); if (darkmode === 'false') { darkmode = true; localStorage.setItem('darkmode', darkmode); } else { darkmode = false; localStorage.setItem('darkmode', darkmode); } if (darkmode) { document.body.classList.add('darkmode'); } else { document.body.classList.remove('darkmode'); } };
|
themes/icarus/source/css/darkmode.css1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| :root { --active-color: #79acff; --text-color: #dddddd; --background-color: #333; --card-background-color: #777777ad; }
.darkmode a.navbar-item, .darkmode .title, .darkmode .content, .darkmode .content>h1, .darkmode .content>h2, .darkmode .content>h3, .darkmode .content>h4, .darkmode .content>h5, .darkmode .content>h6, .darkmode .content>p, .darkmode .level { color: var(--text-color); }
.darkmode nav, .darkmode .navbar-menu { background-color: var(--background-color); }
.darkmode a.is-active { color: var(--active-color); background-color: var(--card-background-color); }
.darkmode a.navbar-item:hover, .darkmode a.level:hover { background-color: var(--card-background-color); }
.darkmode { background-color: var(--card-background-color); }
.darkmode .card-content, .darkmode .box { background-color: var(--background-color); color: var(--text-color); }
.darkmode .timeline .media:last-child:after { background: transparent; }
.darkmode .link-muted { color: var(--text-color); }
.darkmode #back-to-top { background-color: #0807077d; color: var(--text-color); }
.darkmode footer { background-color: var(--background-color); color: var(--text-color); }
|