-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
30 lines (28 loc) · 1.13 KB
/
script.js
File metadata and controls
30 lines (28 loc) · 1.13 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
const darkModeToggle = document.getElementById('darkModeToggle');
const bodyElement = document.body;
const imgDarkMode = document.getElementById('imgDarkMode');
const imgLightMode = document.getElementById('imgLightMode');
// Check if the user has a preferred color scheme and set the toggle accordingly
if (localStorage.getItem('darkMode') === 'enabled') {
bodyElement.classList.add('dark-mode');
darkModeToggle.checked = true;
imgDarkMode.style.display = 'block';
imgLightMode.style.display = 'none';
} else {
imgDarkMode.style.display = 'none';
imgLightMode.style.display = 'block';
}
// Toggle dark mode when the switch is clicked
darkModeToggle.addEventListener('change', () => {
if (darkModeToggle.checked) {
bodyElement.classList.add('dark-mode');
localStorage.setItem('darkMode', 'enabled');
imgDarkMode.style.display = 'block';
imgLightMode.style.display = 'none';
} else {
bodyElement.classList.remove('dark-mode');
localStorage.setItem('darkMode', 'disabled');
imgDarkMode.style.display = 'none';
imgLightMode.style.display = 'block';
}
});