mirror of
https://github.com/hyzendust/hyzendust.github.io.git
synced 2026-04-15 21:28:32 +02:00
25 lines
673 B
HTML
25 lines
673 B
HTML
<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>
|