hugo-battheme/static/js/darkmode.js

33 lines
715 B
JavaScript

const checkbox = document.getElementById("darkmode-toggle");
const colorswitchers = document.getElementsByClassName("colorswitch");
if (localStorage.getItem("dark")) {
switchToDark();
} else {
switchToLight();
}
checkbox.checked = localStorage.getItem("dark");
checkbox.addEventListener("change", function () {
localStorage.setItem("dark", this.checked);
if (this.checked) {
switchToDark();
} else {
localStorage.removeItem("dark");
switchToLight();
}
});
function switchToLight() {
for (let item of colorswitchers) {
item.classList.add("light");
}
}
function switchToDark() {
for (let item of colorswitchers) {
console.log(item);
item.classList.remove("light");
}
}