英文:
Theme toggle button (js) does not work on very first page load
问题
我正在构建一个非常简单的GitHub页面作为简历(我不是Web开发人员),但在首次加载网站时,出现了一个奇怪的问题。
我有一个切换按钮,可以切换主题为暗/亮。当我首次访问这个网站(或在清除缓存和Cookie后再次访问)时,我可以切换到亮主题,但无法切换回暗主题(原始主题)。刷新网站后,一切都正常。
我使用JS来切换主题(但代码本身似乎不是问题):
// 从本地存储中检查用户的主题首选项
const currentTheme = localStorage.getItem('theme');
if (currentTheme) {
body.classList.add(currentTheme);
updateThemeLabel(currentTheme);
}
themeToggle.addEventListener('change', () => {
if (body.classList.contains('light-mode')) {
// 如果当前主题是亮主题,请切换到暗主题
body.classList.replace('light-mode', 'dark-mode');
updateThemeLabel('dark-mode'); // 更新主题标签的文本和颜色
localStorage.setItem('theme', 'dark-mode');
} else {
// 如果当前主题是暗主题,请切换到亮主题
body.classList.replace('dark-mode', 'light-mode');
updateThemeLabel('light-mode'); // 更新主题标签的文本和颜色
localStorage.setItem('theme', 'light-mode');
}
});
function updateThemeLabel(theme) {
if (theme === 'dark-mode') {
themeLabel.textContent = '炫目模式';
themeLabel.style.color = '#fff';
footer.style.color = '#fff';
footer.style.backgroundColor = '#333';
body.style.backgroundColor = '#333';
body.style.color = '#fff';
socialIconTwitter.style.color = '#fff';
socialIconLinkedin.style.color = '#fff';
} else {
themeLabel.textContent = '快速返回!';
themeLabel.style.color = '#000';
footer.style.color = '#000';
footer.style.backgroundColor = '#fff';
body.style.backgroundColor = '#fff';
body.style.color = '#000';
socialIconTwitter.style.color = '#000';
socialIconLinkedin.style.color = '#000';
}
}
希望这对你有所帮助。
英文:
I am building a very simple GitHub page as a CV (I am not a web developer) and I have a strange issue with the behavior of website when it is loaded very first time.
I have a toggle button that switches themes to dark/light. When I visit this website very first time (or after cache a cookies clearing) I can switch to light but not back to dark (original). After refresh of website everything works just fine.
I use JS to toggle theme (but code itself does not seem to be a problem):
// Check the user's theme preference from local storage
const currentTheme = localStorage.getItem('theme');
if (currentTheme) {
body.classList.add(currentTheme);
updateThemeLabel(currentTheme);
}
themeToggle.addEventListener('change', () => {
if (body.classList.contains('light-mode')) {
// If the current theme is light, switch to dark
body.classList.replace('light-mode', 'dark-mode');
updateThemeLabel('dark-mode'); // Update the theme label text and color
localStorage.setItem('theme', 'dark-mode');
} else {
// If the current theme is dark, switch to light
body.classList.replace('dark-mode', 'light-mode');
updateThemeLabel('light-mode'); // Update the theme label text and color
localStorage.setItem('theme', 'light-mode');
}
});
function updateThemeLabel(theme) {
if (theme === 'dark-mode') {
themeLabel.textContent = 'Burn your eyes';
themeLabel.style.color = '#fff';
footer.style.color = '#fff';
footer.style.backgroundColor = '#333';
body.style.backgroundColor = '#333';
body.style.color = '#fff';
socialIconTwitter.style.color = '#fff';
socialIconLinkedin.style.color = '#fff';
} else {
themeLabel.textContent = 'Quickly back!';
themeLabel.style.color = '#000';
footer.style.color = '#000';
footer.style.backgroundColor = '#fff';
body.style.backgroundColor = '#fff';
body.style.color = '#000';
socialIconTwitter.style.color = '#000';
socialIconLinkedin.style.color = '#000';
}
}
答案1
得分: 1
这始终为false
:
if (body.classList.contains('light-mode'))
直到用户选择了某些内容 并重新加载页面,在此时你在此处设置一个类:
body.classList.add(currentTheme);
但是你从不在change
事件处理程序中添加该类。相反,你这样做:
body.classList.replace('dark-mode', 'light-mode');
但如果classList
不包含 'dark-mode'
,那么就没有什么可以替换的了。
取而代之,删除和添加类。例如:
themeToggle.addEventListener('change', () => {
if (body.classList.contains('light-mode')) {
body.classList.remove('light-mode'); // 移除浅色模式
body.classList.add('dark-mode'); // 添加深色模式
updateThemeLabel('dark-mode');
localStorage.setItem('theme', 'dark-mode');
} else {
body.classList.remove('dark-mode'); // 移除深色模式
body.classList.add('light-mode'); // 添加浅色模式
updateThemeLabel('light-mode');
localStorage.setItem('theme', 'light-mode');
}
});
英文:
This is always false
:
if (body.classList.contains('light-mode'))
Until the user selects something and reloads the page, at which time you set a class here:
body.classList.add(currentTheme);
But you never add that class in the change
event handler. Instead, you do this:
body.classList.replace('dark-mode', 'light-mode');
But if the classList
doesn't contain 'dark-mode'
then there's nothing to replace.
Instead, remove and add the classes. For example:
themeToggle.addEventListener('change', () => {
if (body.classList.contains('light-mode')) {
body.classList.remove('light-mode'); // remove light mode
body.classList.add('dark-mode'); // add dark mode
updateThemeLabel('dark-mode');
localStorage.setItem('theme', 'dark-mode');
} else {
body.classList.remove('dark-mode'); // remove dark mode
body.classList.add('light-mode'); // add light mode
updateThemeLabel('light-mode');
localStorage.setItem('theme', 'light-mode');
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论