Skip to content

Commit acf3fff

Browse files
Merge pull request #1116 from PATILYASHH/main
Added Local storage to dark mode light mode
2 parents 5d9299c + 3bb88e1 commit acf3fff

File tree

1 file changed

+37
-7
lines changed

1 file changed

+37
-7
lines changed

index.html

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -288,21 +288,51 @@ <h1>
288288
</script>
289289
<script src="https://cdn.gtranslate.net/widgets/latest/popup.js" defer></script>
290290
</li>
291-
<!-- <div class="theme-switch themeSwitch" id="theme-switch"></div> -->
292-
293291
<div id="themeSwitch" class="theme-switch">
294292
<input type="checkbox" class="checkbox" id="checkbox">
295293
<label for="checkbox" class="checkbox-label">
296-
297294
<img src="./Assets/sun.png" class="theme-btn">
298295
<img src="./Assets/moon.png" class="theme-btn">
299-
<!-- <i class="fas fa-moon"></i>
300-
<i class="fas fa-sun"></i> -->
301296
<span class="ball"></span>
302297
</label>
303298
</div>
304-
</ul>
305-
</nav>
299+
300+
<script>
301+
// Function to toggle the theme and update local storage
302+
const toggleTheme = () => {
303+
const checkbox = document.getElementById("checkbox");
304+
if (checkbox.checked) {
305+
document.body.classList.add("dark-mode"); // Update to match your CSS
306+
localStorage.setItem("theme", "dark"); // Save theme to localStorage
307+
} else {
308+
document.body.classList.remove("dark-mode"); // Update to match your CSS
309+
localStorage.setItem("theme", "light"); // Save theme to localStorage
310+
}
311+
};
312+
313+
// Load the theme from local storage on page load
314+
const loadTheme = () => {
315+
const theme = localStorage.getItem("theme");
316+
const checkbox = document.getElementById("checkbox");
317+
318+
// Check the stored theme and apply it
319+
if (theme === "dark") {
320+
checkbox.checked = true; // Set checkbox state
321+
document.body.classList.add("dark-mode"); // Apply dark theme
322+
} else {
323+
checkbox.checked = false; // Set checkbox state
324+
document.body.classList.remove("dark-mode"); // Apply light theme
325+
}
326+
};
327+
328+
// Event listener for the checkbox
329+
document.getElementById("checkbox").addEventListener("change", toggleTheme);
330+
331+
// Call the function to load the theme when the page loads
332+
window.onload = loadTheme;
333+
</script>
334+
</ul>
335+
</nav>
306336

307337
</header>
308338

0 commit comments

Comments
 (0)