I'm having a problem with my website, when I refrech a web page and the dark theme enabled, it blinks to white for less than one sec and back to dark
so how to fix that problem
my js code,
/*==================== DARK LIGHT THEME ====================*/
const themeButton = document.getElementById('theme-button')
const darkTheme = 'dark-theme'
const iconTheme = 'bx-sun'
// Previously selected topic (if user selected)
const selectedTheme = localStorage.getItem('selected-theme')
const selectedIcon = localStorage.getItem('selected-icon')
// We obtain the current theme that the interface has by validating the dark-theme class
const getCurrentTheme = () => document.body.classList.contains(darkTheme) ? 'dark' : 'light'
const getCurrentIcon = () => themeButton.classList.contains(iconTheme) ? 'bx-moon' : 'bx-sun'
// We validate if the user previously chose a topic
if (selectedTheme) {
// If the validation is fulfilled, we ask what the issue was to know if we activated or deactivated the dark
document.body.classList[selectedTheme === 'dark' ? 'add' : 'remove'](darkTheme)
themeButton.classList[selectedIcon === 'bx-moon' ? 'add' : 'remove'](iconTheme)
}
// Activate / deactivate the theme manually with the button
themeButton.addEventListener('click', () => {
// Add or remove the dark / icon theme
document.body.classList.toggle(darkTheme)
themeButton.classList.toggle(iconTheme)
// We save the theme and the current icon that the user chose
localStorage.setItem('selected-theme', getCurrentTheme())
localStorage.setItem('selected-icon', getCurrentIcon())
})
/*==================== SCROLL REVEAL ANIMATION ====================*/
const sr = ScrollReveal({
origin: 'top',
distance: '30px',
duration: 1000,
reset: true
});
sr.reveal(` .home__data, .home__img,
.about__data, .about__img,
.services__content, .menu__content,
.app__data, .app__img,
.contact__data, .contact__button,
.footer__content`, {
interval: 200
})
question from:
https://stackoverflow.com/questions/65835201/how-to-prevent-website-blinks-to-white-theme-when-dark-theme-enabled 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…