js
This commit is contained in:
parent
e395348e62
commit
aec7a3fd61
1 changed files with 19 additions and 24 deletions
|
@ -1,42 +1,37 @@
|
||||||
const buttons = ["item", "link", "swap"];
|
const buttons = ["item", "link", "swap"];
|
||||||
|
|
||||||
// toggle buttons
|
|
||||||
|
|
||||||
for (let button of buttons) {
|
for (let button of buttons) {
|
||||||
document.getElementById(button).addEventListener("click", () => {
|
document.getElementById(button).addEventListener("click", () => {
|
||||||
document.body.classList.toggle(button);
|
document.body.classList.toggle(button);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// theme
|
let theme
|
||||||
|
|
||||||
const button = document.getElementById("theme");
|
|
||||||
|
|
||||||
function theme_get() {
|
function theme_get() {
|
||||||
return document.documentElement.getAttribute("data-theme");
|
theme = localStorage.getItem("theme");
|
||||||
}
|
if (! theme) {
|
||||||
function theme_load() {
|
if (window.matchMedia("(prefers-color-scheme: dark)").matches) {
|
||||||
const theme = localStorage.getItem("theme");
|
theme = "dark";
|
||||||
if (theme) {
|
} else {
|
||||||
theme_set(theme);
|
theme = "light";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
theme_set()
|
||||||
}
|
}
|
||||||
function theme_set(theme) {
|
|
||||||
|
function theme_set() {
|
||||||
document.documentElement.setAttribute("data-theme", theme);
|
document.documentElement.setAttribute("data-theme", theme);
|
||||||
theme_write(theme);
|
|
||||||
}
|
|
||||||
function theme_swap(theme) {
|
|
||||||
return theme === "dark" ? "light" : "dark";
|
|
||||||
}
|
|
||||||
function theme_toggle() {
|
|
||||||
theme_set(theme_swap(theme_get()));
|
|
||||||
}
|
|
||||||
function theme_write(theme) {
|
|
||||||
localStorage.setItem("theme", theme);
|
localStorage.setItem("theme", theme);
|
||||||
}
|
}
|
||||||
|
|
||||||
button.addEventListener("click", () => {
|
function theme_swap() {
|
||||||
theme_toggle();
|
theme = theme === "light" ? "dark" : "light";
|
||||||
|
theme_set();
|
||||||
|
}
|
||||||
|
|
||||||
|
document.getElementById("theme").addEventListener("click", () => {
|
||||||
|
theme_swap();
|
||||||
});
|
});
|
||||||
|
|
||||||
theme_load();
|
theme_get()
|
||||||
|
|
Loading…
Add table
Reference in a new issue