hugo-battheme/static/js/darkmode.js

33 lines
714 B
JavaScript
Raw Normal View History

const checkbox = document.getElementById("darkmode-toggle");
const colorswitchers = document.getElementsByClassName("colorswitch");
2022-04-09 19:42:12 +00:00
if (localStorage.getItem("dark")) {
switchToDark();
2022-04-09 20:23:18 +00:00
} else {
switchToLight();
2022-04-09 19:42:12 +00:00
}
checkbox.checked = localStorage.getItem("dark");
2022-04-09 20:23:18 +00:00
checkbox.addEventListener("click", function () {
localStorage.setItem("dark", this.checked);
if (this.checked) {
switchToDark();
} else {
localStorage.removeItem("dark");
switchToLight();
}
});
2022-04-09 19:42:12 +00:00
function switchToLight() {
for (let item of colorswitchers) {
item.classList.add("light");
}
2022-04-09 19:42:12 +00:00
}
function switchToDark() {
for (let item of colorswitchers) {
console.log(item);
item.classList.remove("light");
}
2022-04-09 19:42:12 +00:00
}