首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在hugo中根据主题显示不同的图像?

如何在hugo中根据主题显示不同的图像?
EN

Stack Overflow用户
提问于 2020-12-29 20:23:15
回答 1查看 85关注 0票数 0

我在Hugo建了一个网站。我使用的是hello friend ng主题。我想要添加一个svg图像到主站点,但我希望它根据所选主题是亮还是暗而改变。

该主题切换由theme.js处理

代码语言:javascript
复制
const theme = window.localStorage && window.localStorage.getItem("theme");
const themeToggle = document.querySelector(".theme-toggle");
const isDark = theme === "dark";
var metaThemeColor = document.querySelector("meta[name=theme-color]");

if (theme !== null) {
  document.body.classList.toggle("dark-theme", isDark);
  isDark
    ? metaThemeColor.setAttribute("content", "#252627")
    : metaThemeColor.setAttribute("content", "#fafafa");
}

themeToggle.addEventListener("click", () => {
  document.body.classList.toggle("dark-theme");
  window.localStorage &&
    window.localStorage.setItem(
      "theme",
      document.body.classList.contains("dark-theme") ? "dark" : "light"
    );
  document.body.classList.contains("dark-theme")
    ? metaThemeColor.setAttribute("content", "#252627")
    : metaThemeColor.setAttribute("content", "#fafafa");
});

但我不知道如何访问它正在使用的变量。

EN

回答 1

Stack Overflow用户

发布于 2020-12-30 18:46:13

我没有做到这一点,但是我找到了一个满足我需求的变通方法,所以我把它贴出来,以防有人觉得它很有价值。

我没有使用链接到不同svg图像的<img>,而是决定使用<svg>并将我的图像内容复制到那里,因为它只是xml。然后,我删除了strokefill等属性,用一个自定义类将该<svg>包装在一个<div中,然后由css进行控制。

我不确定这是不是最好的方法,但它似乎对我有效。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65492079

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档