-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscript.js
More file actions
34 lines (29 loc) · 950 Bytes
/
script.js
File metadata and controls
34 lines (29 loc) · 950 Bytes
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
const themeToggle = document.getElementById('theme-toggle');
const themeIcon = document.getElementById('theme-icon');
const body = document.body;
function darkMode() {
document.body.classList.add("dark-mode");
document.body.classList.remove("light-mode");
}
function lightMode() {
document.body.classList.add("light-mode");
document.body.classList.remove("dark-mode");
}
// Optionally, store the preference in localStorage so that the theme persists on page reload
window.addEventListener("DOMContentLoaded", (event) => {
if (localStorage.getItem("theme") === "dark") {
darkMode();
} else {
lightMode();
}
});
function toggleTheme() {
if (document.body.classList.contains("dark-mode")) {
lightMode();
localStorage.setItem("theme", "light");
} else {
darkMode();
localStorage.setItem("theme", "dark");
}
}
themeToggle.addEventListener('click', toggleTheme);