hugo-battheme/static/js/darkmode.js

33 lines
718 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
2022-04-13 10:43:01 +00:00
if (localStorage.getItem("light")) {
switchToLight();
2022-04-13 10:43:01 +00:00
} else {
switchToDark();
2022-04-09 19:42:12 +00:00
}
checkbox.checked = localStorage.getItem("dark");
2022-04-09 20:23:18 +00:00
2022-04-10 19:57:17 +00:00
checkbox.addEventListener("change", function () {
2022-04-13 10:43:01 +00:00
localStorage.setItem("light", this.checked);
if (this.checked) {
switchToLight();
2022-04-13 10:43:01 +00:00
} else {
localStorage.removeItem("light");
switchToDark();
}
});
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
}