hugo-batsite/public/js/lightmode.js

44 lines
1 KiB
JavaScript
Raw Normal View History

2022-04-16 11:18:04 +00:00
const checkbox = document.getElementById("darkmode-toggle");
const colorswitchers = document.getElementsByClassName("colorswitch");
2022-04-16 13:27:59 +00:00
const button = document.getElementById("darkmode-button-small");
2022-04-16 11:18:04 +00:00
function switchToLight() {
2022-04-16 13:27:59 +00:00
button.innerHTML = "滛";
2022-04-16 11:18:04 +00:00
for (let item of colorswitchers) {
item.classList.add("light");
}
}
function switchToDark() {
2022-04-16 13:27:59 +00:00
button.innerHTML = "";
2022-04-16 11:18:04 +00:00
for (let item of colorswitchers) {
console.log(item);
item.classList.remove("light");
}
}
export function updateMode() {
if (localStorage.getItem("light")) {
switchToLight();
} else {
switchToDark();
}
checkbox.checked = localStorage.getItem("dark");
2022-04-16 13:27:59 +00:00
button.addEventListener("click", function () {
checkbox.click();
});
2022-04-16 11:18:04 +00:00
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;
}
}