hugo-battheme/static/js/darkmode.js

32 lines
714 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("click", 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");
}
}