-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
72 lines (60 loc) · 1.92 KB
/
script.js
File metadata and controls
72 lines (60 loc) · 1.92 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* SCRIPT THEME */
const $html = document.querySelector("html");
const themeToggle = document.getElementById("theme-toggle");
function toggleTheme() {
$html.classList.toggle("light-mode");
updateLocalStorage();
updateIcons();
updateStarsVisibility();
updateBackground();
}
function updateLocalStorage() {
localStorage.setItem(
"themePreference",
$html.classList.contains("light-mode") ? "light" : "dark"
);
}
function loadThemePreference() {
const themePreference = localStorage.getItem("themePreference");
if (themePreference === "light") {
$html.classList.add("light-mode");
}
}
loadThemePreference();
function updateIcons() {
const moonIcon = document.querySelector(".fa-moon");
const sunIcon = document.querySelector(".fa-sun");
if ($html.classList.contains("light-mode")) {
moonIcon.style.display = "none";
sunIcon.style.display = "inline";
} else {
moonIcon.style.display = "inline";
sunIcon.style.display = "none";
}
}
updateIcons();
updateStarsVisibility();
updateBackground();
function updateStarsVisibility() {
const stars1 = document.getElementById("stars1");
const stars2 = document.getElementById("stars2");
const stars3 = document.getElementById("stars3");
if ($html.classList.contains("light-mode")) {
stars1.style.display = "none";
stars2.style.display = "none";
stars3.style.display = "none";
} else {
stars1.style.display = "block";
stars2.style.display = "block";
stars3.style.display = "block";
}
}
function updateBackground() {
const root = document.documentElement;
if ($html.classList.contains("light-mode")) {
root.style.setProperty('--bg-image', 'url("./images/cloud.svg")');
} else {
root.style.setProperty('--bg-image', 'radial-gradient(ellipse at bottom, #1b2735 0%, #090a0f 100%)');
}
}
themeToggle.addEventListener("click", toggleTheme);