【发布时间】:2020-12-30 18:46:13
【问题描述】:
我正在 Hugo 中建立一个站点。我正在使用hello friend ng 主题。我想向主站点添加一个 svg 图像,但我希望它根据所选主题是浅色还是深色而改变。
主题切换由theme.js处理:
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");
});
但我不知道如何访问它正在使用的变量。
【问题讨论】:
标签: javascript html themes hugo