33 lines
857 B
JavaScript
33 lines
857 B
JavaScript
|
|
document.addEventListener('DOMContentLoaded', function () {
|
|
|
|
var btn = document.getElementById('themeToggle');
|
|
var icon = document.getElementById('themeIcon');
|
|
|
|
if (!btn || !icon) return;
|
|
|
|
var root = document.documentElement;
|
|
|
|
var savedTheme = localStorage.getItem('theme') || 'light';
|
|
|
|
root.setAttribute('data-theme', savedTheme);
|
|
|
|
updateIcon(savedTheme);
|
|
|
|
btn.addEventListener('click', function () {
|
|
var current = root.getAttribute('data-theme');
|
|
var next = current === 'light' ? 'dark' : 'light';
|
|
root.setAttribute('data-theme', next);
|
|
localStorage.setItem('theme', next);
|
|
updateIcon(next);
|
|
});
|
|
|
|
function updateIcon(theme) {
|
|
if (theme === 'dark') {
|
|
icon.textContent = '☀';
|
|
} else {
|
|
icon.textContent = '☾';
|
|
}
|
|
}
|
|
});
|