mirror of
https://github.com/Rainyy21/Rainyy21.github.io.git
synced 2026-10-11 00:30:17 -04:00
add a template for project and try to fix darkmode
This commit is contained in:
+24
-18
@@ -1,21 +1,27 @@
|
||||
let darkmode = localStorage.getItem('dark');
|
||||
const switch_button = document.getElementById("theme_switch");
|
||||
|
||||
const enableDarkmode = () => {
|
||||
document.body.classList.add('dark');
|
||||
localStorage.setItem('dark' , 'active');
|
||||
}
|
||||
|
||||
const disableDarkmode = () => {
|
||||
document.body.classList.remove('dark');
|
||||
localStorage.removeItem('dark');
|
||||
// Function to apply the theme based on localStorage
|
||||
const applyTheme = () => {
|
||||
const darkmode = localStorage.getItem('dark');
|
||||
if (darkmode === 'active') {
|
||||
document.body.classList.add('dark');
|
||||
} else {
|
||||
document.body.classList.remove('dark');
|
||||
}
|
||||
};
|
||||
|
||||
if (darkmode === 'active') enableDarkmode();
|
||||
|
||||
switch_button.addEventListener("click", () => {
|
||||
darkmode = localStorage.getItem('dark');
|
||||
darkmode !== "active" ? enableDarkmode() : disableDarkmode();
|
||||
});
|
||||
|
||||
// Run on initial load
|
||||
// Since the script is 'defer', document.body will be available
|
||||
applyTheme();
|
||||
|
||||
// Set up the toggle button listener
|
||||
const switch_button = document.getElementById("theme_switch");
|
||||
if (switch_button) {
|
||||
switch_button.addEventListener("click", () => {
|
||||
const darkmode = localStorage.getItem('dark');
|
||||
if (darkmode !== "active") {
|
||||
localStorage.setItem('dark', 'active');
|
||||
} else {
|
||||
localStorage.setItem('dark', 'inactive');
|
||||
}
|
||||
applyTheme();
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user