-
Notifications
You must be signed in to change notification settings - Fork 0
/
darkmode.js
39 lines (36 loc) · 1.23 KB
/
darkmode.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// variables
let moonBtn = document.getElementById("moonBtn");
let sunBtn = document.getElementById("sunBtn");
const themeBtnIcon = document.getElementById("themeBtnIcon");
const body = document.body;
// funciones
const sunButton = () => {
moonBtn.style.display = "none";
sunBtn.style.display = "block";
};
const moonButton = () => {
sunBtn.style.display = "none";
moonBtn.style.display = "block";
};
const makeDark = () => {
body.classList.replace("light", "dark");
localStorage.setItem("theme", "dark");
sunButton();
console.log("darkified");
};
const makeLight = () => {
body.classList.replace("dark", "light");
localStorage.setItem("theme", "light");
moonButton();
console.log("lightified");
};
// onclick events del botón (Sí, son dos botones que se ocultan y muestran para parecer uno)
moonBtn.onclick = makeDark;
sunBtn.onclick = makeLight;
// condiciones para que se mantenga la oscuridad mediante cache localstorage
window.onload = () => {
localStorage.getItem("theme") || localStorage.setItem("theme", "light")
body.classList.replace("light", localStorage.getItem("theme"));
body.classList.replace("dark", localStorage.getItem("theme"));
localStorage.getItem("theme") === "dark" ? sunButton() : moonButton();
};