Hi I need to know how I can add a transition effect when my theme stylesheet changes when clicking on the button. The following is my code.
document.addEventListener('DOMContentLoaded', () => {
const themeStylesheet = document.getElementById('theme');
const storedTheme = localStorage.getItem('theme');
if (storedTheme) {
themeStylesheet.href = storedTheme;
}
const themeToggle = document.getElementById('theme-toggley');
themeToggle.addEventListener('click', () => {
// if it's light -> go dark
if (themeStylesheet.href.includes('light')) {
themeStylesheet.href = 'css/dark-theme.css';
} else {
// if it's dark -> go light
themeStylesheet.href = 'css/light-theme.css';
}
// save the preference to localStorage
localStorage.setItem('theme', themeStylesheet.href)
})
})
<link id="theme" rel="stylesheet" type="text/css" href="css/light-theme.css" />
<button id="theme-toggley">Light</button>