-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnavbar.js
48 lines (42 loc) · 1.61 KB
/
navbar.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
// Function to load navbar content
function loadNavbarContent() {
const shortnav = document.querySelector('.navbar');
if (shortnav) {
fetch('navbar.html')
.then(res => res.text())
.then(data => {
shortnav.innerHTML = data;
const LightImage = document.getElementById("Lightbulb");
const DarkImage = document.getElementById("Darkbulb");
// Check if the user preference is already stored in localStorage
const isDarkMode = localStorage.getItem('darkMode') === 'true';
// Apply the user's preference
if (isDarkMode) {
document.body.classList.add('dark-mode');
LightImage.classList.add("hide");
DarkImage.classList.remove("hide");
}
if (LightImage) {
// Add event listener to the bulb image
LightImage.addEventListener('click', () => {
document.body.classList.add('dark-mode');
LightImage.classList.add("hide");
DarkImage.classList.remove("hide");
// Store user preference in localStorage
localStorage.setItem('darkMode', 'true');
});
}
if (DarkImage) {
// Add event listener to the bulb image
DarkImage.addEventListener('click', () => {
document.body.classList.remove('dark-mode');
LightImage.classList.remove("hide");
DarkImage.classList.add("hide");
// Store user preference in localStorage
localStorage.setItem('darkMode', 'false');
});
}
});
}
}
document.addEventListener('DOMContentLoaded', loadNavbarContent);