fix lightmode button being weird

This commit is contained in:
zoe 2022-04-13 16:00:22 +02:00
parent 76b758dcad
commit 2e52a4843f
4 changed files with 42 additions and 36 deletions

View file

@ -1,23 +1,6 @@
const checkbox = document.getElementById("darkmode-toggle");
const colorswitchers = document.getElementsByClassName("colorswitch");
if (localStorage.getItem("light")) {
switchToLight();
} else {
switchToDark();
}
checkbox.checked = localStorage.getItem("dark");
checkbox.addEventListener("change", function () {
localStorage.setItem("light", this.checked);
if (this.checked) {
switchToLight();
} else {
localStorage.removeItem("light");
switchToDark();
}
});
function switchToLight() {
for (let item of colorswitchers) {
item.classList.add("light");
@ -30,3 +13,24 @@ function switchToDark() {
item.classList.remove("light");
}
}
document.addEventListener("DOMContentLoaded", () => {
if (localStorage.getItem("light")) {
switchToLight();
} else {
switchToDark();
}
checkbox.checked = localStorage.getItem("dark");
checkbox.addEventListener("change", function () {
localStorage.setItem("light", this.checked);
if (this.checked) {
switchToLight();
} else {
localStorage.removeItem("light");
switchToDark();
}
});
if (localStorage.getItem("light")) {
checkbox.checked = true;
}
});