-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlightDarkMode.js
122 lines (96 loc) · 3.52 KB
/
lightDarkMode.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
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Get references to the light mode and dark mode icons
const lightModeIcon = document.getElementById("light-mode");
const darkModeIcon = document.getElementById("dark-mode");
const icons = document.getElementsByClassName("icon");
const graphics = document.getElementsByClassName("graphics-backgrounds");
const body = document.body;
const p = document.querySelectorAll("p");
const a = document.querySelectorAll("a")
const details_container = document.getElementsByClassName("details-container");
const h2_underline = document.getElementsByClassName("underline");
const download_icon = document.getElementsByClassName("download")
const profilePic = document.getElementById("profile-pic");
const toggleSvg = document.getElementById("toggle");
const localMode = localStorage.getItem("mode");
if (localMode === "light") {
enableDarkMode();
}
else if (localMode === "dark") {
enableLightMode();
}
// Function to enable dark mode
function enableDarkMode() {
lightModeIcon.style.display = "none";
darkModeIcon.style.display = "block";
// add icon-dark class to icons
for (let i = 0; i < icons.length; i++) {
icons[i].classList.add("icon-dark");
}
body.classList.toggle('dark-mode');
for (let i = 0; i < graphics.length; i++) {
graphics[i].classList.add("dark-graphics-backgrounds");
}
p.forEach(function (paragraph) {
paragraph.classList.add('dark-mode')
});
a.forEach(function (anquor) {
anquor.classList.add('dark-mode')
});
for (let i = 0; i < details_container.length; i++) {
details_container[i].classList.add('details-container-dark')
}
for (let i = 0; i < h2_underline.length; i++) {
h2_underline[i].classList.add('underline-dark-mode')
}
for (let i = 0; i < download_icon.length; i++) {
download_icon[i].classList.add('download-dark')
}
// Store the current mode in localStorage
localStorage.setItem("mode", "dark");
}
// Function to enable light mode
function enableLightMode() {
darkModeIcon.style.display = "none";
lightModeIcon.style.display = "block";
// remove icon-dark class to icons
for (let i = 0; i < icons.length; i++) {
icons[i].classList.remove("icon-dark");
}
body.classList.remove('dark-mode');
for (let i = 0; i < graphics.length; i++) {
graphics[i].classList.remove("dark-graphics-backgrounds");
}
p.forEach(function (paragraph) {
paragraph.classList.remove('dark-mode')
});
a.forEach(function (anquor) {
anquor.classList.remove('dark-mode')
});
for (let i = 0; i < details_container.length; i++) {
details_container[i].classList.remove('details-container-dark')
}
for (let i = 0; i < h2_underline.length; i++) {
h2_underline[i].classList.remove('underline-dark-mode')
}
for (let i = 0; i < download_icon.length; i++) {
download_icon[i].classList.remove('download-dark')
}
// Store the current mode in localStorage
localStorage.setItem("mode", "light");
}
// Toggle function for dark/light mode
function toggleMode() {
// Check the current mode in localStorage
const currentMode = localStorage.getItem("mode");
if (currentMode === "dark") {
// Enable light mode
enableLightMode();
} else {
// Enable dark mode
enableDarkMode();
}
}
// Toggle mode when clicking the icons
lightModeIcon.addEventListener("click", toggleMode);
darkModeIcon.addEventListener("click", toggleMode);
toggleSvg.addEventListener("click", toggleMode);