abgame/static/js/toggle.js

35 lines
1.1 KiB
JavaScript
Raw Normal View History

2023-06-04 01:19:02 +02:00
function initToggle() {
const toggleSwitch = document.querySelector('.toggle input[type="checkbox"]');
const currentTheme = localStorage.getItem('theme') ? localStorage.getItem('theme') : null;
function switchTheme(e) {
if (e.target.checked) {
document.documentElement.setAttribute('data-theme', 'dark');
2023-06-04 01:26:41 +02:00
localStorage.setItem('theme', 'dark');
2023-06-04 01:19:02 +02:00
}
else {
document.documentElement.setAttribute('data-theme', 'light');
2023-06-04 01:26:41 +02:00
localStorage.setItem('theme', 'light');
2023-06-04 01:19:02 +02:00
}
}
toggleSwitch.addEventListener('change', switchTheme, false);
if (currentTheme) {
document.documentElement.setAttribute('data-theme', currentTheme);
if (currentTheme === 'dark') {
toggleSwitch.checked = true;
} else {
toggleSwitch.checked = false;
}
}
document.querySelectorAll('.spacer').forEach(function (el) {
el.style.display = 'none';
});
document.querySelectorAll('.toggle').forEach(function (el) {
el.style.display = 'unset';
});
2023-06-04 01:26:41 +02:00
}