当页面的类别/主题切换时更新`theme-color`。

huangapple go评论136阅读模式
英文:

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(&quot;html&quot;);
const button = document.querySelector(&quot;.contrast__link&quot;);

button.addEventListener(&quot;click&quot;, (e) =&gt; {
	e.preventDefault();
	if (html.classList.contains(&quot;dark-mode&quot;)) {
		html.classList.remove(&quot;dark-mode&quot;);
		html.classList.add(&quot;retro&quot;);
	} else if (html.classList.contains(&quot;retro&quot;)) {
		html.classList.remove(&quot;retro&quot;);
	} else {
		html.classList.add(&quot;dark-mode&quot;);
	}
});

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:

&lt;meta name=&quot;theme-color&quot; content=&quot;#ffffff&quot;&gt;

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

&lt;meta name=&quot;theme-color&quot; content=&quot;var(--color-primary&quot;&gt;

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(&quot;html&quot;);
const button = document.querySelector(&quot;.contrast__link&quot;);

// 让我们首先定义你的颜色
const colors = {
  default: &#39;#ffffff&#39;, // 默认主题的颜色
  darkMode: &#39;#000000&#39;, // 暗模式主题的颜色
  retro: &#39;#f3eee2&#39; // 复古主题的颜色
}

// 访问meta标签
const metaThemeColor = document.querySelector(&quot;meta[name=theme-color]&quot;);

button.addEventListener(&quot;click&quot;, (e) =&gt; {
  e.preventDefault();
  if (html.classList.contains(&quot;dark-mode&quot;)) {
    html.classList.remove(&quot;dark-mode&quot;);
    html.classList.add(&quot;retro&quot;);
    // 更新theme-color的meta标签
    metaThemeColor.setAttribute(&#39;content&#39;, colors.retro);
  } else if (html.classList.contains(&quot;retro&quot;)) {
    html.classList.remove(&quot;retro&quot;);
    // 更新theme-color的meta标签
    metaThemeColor.setAttribute(&#39;content&#39;, colors.default);
  } else {
    html.classList.add(&quot;dark-mode&quot;);
    // 更新theme-color的meta标签
    metaThemeColor.setAttribute(&#39;content&#39;, 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(&quot;html&quot;);
const button = document.querySelector(&quot;.contrast__link&quot;);
// Let&#39;s start with defining your colors
const colors = {
default: &#39;#ffffff&#39;, // color for the default theme
darkMode: &#39;#000000&#39;, // color for the dark-mode theme
retro: &#39;#f3eee2&#39; // color for the retro theme
}
// Access the meta tag
const metaThemeColor = document.querySelector(&quot;meta[name=theme-color]&quot;);
button.addEventListener(&quot;click&quot;, (e) =&gt; {
e.preventDefault();
if (html.classList.contains(&quot;dark-mode&quot;)) {
html.classList.remove(&quot;dark-mode&quot;);
html.classList.add(&quot;retro&quot;);
// update the theme-color meta tag
metaThemeColor.setAttribute(&#39;content&#39;, colors.retro);
} else if (html.classList.contains(&quot;retro&quot;)) {
html.classList.remove(&quot;retro&quot;);
// update the theme-color meta tag
metaThemeColor.setAttribute(&#39;content&#39;, colors.default);
} else {
html.classList.add(&quot;dark-mode&quot;);
// update the theme-color meta tag
metaThemeColor.setAttribute(&#39;content&#39;, colors.darkMode);
}
});

huangapple
  • 本文由 发表于 2023年6月26日 15:16:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76554339.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定