英文:
Update `theme-color` when class/theme of page is toggled
问题
我有一个JavaScript开关,它在html
元素上添加/移除类来改变我的页面的颜色/主题。第1个(没有类)= 白色,第2个(.dark-mode
)主题是黑色,第3个(.retro
)是米色。
JavaScript开关还有一些额外的主题设置,我已经去掉了它,只为了简化下面的示例:
const html = document.querySelector("html");
const button = document.querySelector(".contrast__link");
button.addEventListener("click", (e) => {
e.preventDefault();
if (html.classList.contains("dark-mode")) {
html.classList.remove("dark-mode");
html.classList.add("retro");
} else if (html.classList.contains("retro")) {
html.classList.remove("retro");
} else {
html.classList.add("dark-mode");
}
});
当类/主题更新时,我想更新文档head
中的theme-color
以匹配页面的其余部分。我的原始标记如下:
<meta name="theme-color" content="#ffffff">
我尝试使用CSS变量,以为它会引入相关的颜色...
CSS
:root { --color-secondary : rgb(255,255,255); }
.dark-mode { --color-secondary : rgb(0,0,0); }
.retro { --color-secondary : rgb(243, 238, 226); }
标记
<meta name="theme-color" content="var(--color-primary)">
但是看起来这不受支持。是否有一种方法可以继承这个theme-color
,还是它需要是一个固定/设置的值?
英文:
I have a Javascript toggle that adds/removes classes on the html
element to change the colour/theme of my page. 1st (no class) = white, the 2nd (.dark-mode
) theme is black, the 3rd (.retro
) is beige.
The Javascript toggle does have some extra theme setting stuff in it which I've tripped out just to streamline the example below:
const html = document.querySelector("html");
const button = document.querySelector(".contrast__link");
button.addEventListener("click", (e) => {
e.preventDefault();
if (html.classList.contains("dark-mode")) {
html.classList.remove("dark-mode");
html.classList.add("retro");
} else if (html.classList.contains("retro")) {
html.classList.remove("retro");
} else {
html.classList.add("dark-mode");
}
});
When the class/theme updates. I'd like to update the theme-color
in the document head
to match the rest of the page. My original tag looked like this:
<meta name="theme-color" content="#ffffff">
I tried using a CSS variable thinking it would pull in the relevant colour...
CSS
:root { --color-secondary : rgb(255,255,255); }
.dark-mode { --color-secondary : rgb(0,0,0); }
.retro { --color-secondary : rgb(243, 238, 226); }
Tag
<meta name="theme-color" content="var(--color-primary">
But it looks like this is not supported. Is there a way for this theme-color
to be inherited or does it need to be a fixed/set value?
答案1
得分: 1
你说得对,<meta name="theme-color"> 内容属性不支持CSS变量。
要根据你的主题更新这个值,你需要使用JavaScript。
const html = document.querySelector("html");
const button = document.querySelector(".contrast__link");
// 让我们首先定义你的颜色
const colors = {
default: '#ffffff', // 默认主题的颜色
darkMode: '#000000', // 暗模式主题的颜色
retro: '#f3eee2' // 复古主题的颜色
}
// 访问meta标签
const metaThemeColor = document.querySelector("meta[name=theme-color]");
button.addEventListener("click", (e) => {
e.preventDefault();
if (html.classList.contains("dark-mode")) {
html.classList.remove("dark-mode");
html.classList.add("retro");
// 更新theme-color的meta标签
metaThemeColor.setAttribute('content', colors.retro);
} else if (html.classList.contains("retro")) {
html.classList.remove("retro");
// 更新theme-color的meta标签
metaThemeColor.setAttribute('content', colors.default);
} else {
html.classList.add("dark-mode");
// 更新theme-color的meta标签
metaThemeColor.setAttribute('content', colors.darkMode);
}
});
英文:
You're correct that the <meta name="theme-color"> content attribute doesn't support CSS variables.
To update this value based on your theme, you will have to use JavaScript.
const html = document.querySelector("html");
const button = document.querySelector(".contrast__link");
// Let's start with defining your colors
const colors = {
default: '#ffffff', // color for the default theme
darkMode: '#000000', // color for the dark-mode theme
retro: '#f3eee2' // color for the retro theme
}
// Access the meta tag
const metaThemeColor = document.querySelector("meta[name=theme-color]");
button.addEventListener("click", (e) => {
e.preventDefault();
if (html.classList.contains("dark-mode")) {
html.classList.remove("dark-mode");
html.classList.add("retro");
// update the theme-color meta tag
metaThemeColor.setAttribute('content', colors.retro);
} else if (html.classList.contains("retro")) {
html.classList.remove("retro");
// update the theme-color meta tag
metaThemeColor.setAttribute('content', colors.default);
} else {
html.classList.add("dark-mode");
// update the theme-color meta tag
metaThemeColor.setAttribute('content', colors.darkMode);
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论