mirror of
https://github.com/hyzendust/hyzendust.github.io.git
synced 2026-04-16 05:38:33 +02:00
Add: Theme switcher
This commit is contained in:
@@ -1 +1,24 @@
|
||||
{{- /* You can dynamically load scripts in this file */ -}}
|
||||
<script>
|
||||
(function () {
|
||||
var btn = document.getElementById('theme-toggle');
|
||||
if (!btn) return;
|
||||
|
||||
function getTheme() {
|
||||
var stored = localStorage.getItem('theme');
|
||||
if (stored) return stored;
|
||||
return window.matchMedia('(prefers-color-scheme: light)').matches ? 'light' : 'dark';
|
||||
}
|
||||
|
||||
function applyTheme(theme) {
|
||||
document.documentElement.setAttribute('data-theme', theme);
|
||||
localStorage.setItem('theme', theme);
|
||||
}
|
||||
|
||||
applyTheme(getTheme());
|
||||
|
||||
btn.addEventListener('click', function () {
|
||||
var current = document.documentElement.getAttribute('data-theme') || getTheme();
|
||||
applyTheme(current === 'dark' ? 'light' : 'dark');
|
||||
});
|
||||
})();
|
||||
</script>
|
||||
|
||||
@@ -12,4 +12,12 @@
|
||||
<!-- Direct custom CSS -->
|
||||
<link rel="stylesheet" href="{{ "css/custom.css" | relURL }}">
|
||||
|
||||
<!-- Theme switcher: run before paint to avoid flash -->
|
||||
<script>
|
||||
(function(){
|
||||
var t = localStorage.getItem('theme');
|
||||
if (t) { document.documentElement.setAttribute('data-theme', t); }
|
||||
})();
|
||||
</script>
|
||||
|
||||
{{ partial "head/head-end.html" . }}
|
||||
|
||||
Reference in New Issue
Block a user