【发布时间】:2020-07-16 23:29:59
【问题描述】:
我有一个数据主题,其中包含对原始 CSS 主题的大量修改。我已经有一个拨动开关在工作,我将它保存到 localStorage 以便记住它。但是,每当我在已启用深色主题的页面之间切换时,它会首先加载原始主题,但在整个页面加载后 应用深色主题。我希望页面之间的这种过渡是无缝的。
我正在使用 Node.js 并为 Web 应用程序表达。
我的数据主题修改大约有 200 行。这是数据主题的简短 sn-p:
[data-theme="dark"] html {
background-color: #111111 !important;
transition: 0.3s;
}
[data-theme="dark"] .bg-light {
background-color: #333 !important;
transition: 0.3s;
}
[data-theme="dark"] .bg-white {
background-color: #000 !important;
transition: 0.3s;
}
[data-theme="dark"] .bg-black {
background-color: #eee !important;
transition: 0.3s;
}
[data-theme="dark"] .topbar {
background-color: #2196f3 !important;
color: #2a1e37 !important;
transition: 0.3s;
}
[data-theme="dark"] .btn {
background-color: #2196f3 !important;
border: 1px solid #2196f3 !important;
color: #2a1e37 !important;
transition: 0.3s;
}
等等。它适用于我的大部分元素。
我的问题的一个例子显示在这个简短的 gif 中:
https://gyazo.com/d53e12b7b5ea5215659a59a67639ba37
目前,我有一个脚本可以确定是否在标签中应用深色主题或浅色主题。
function initTheme() {
console.log("initTheme() invoked!");
var darkThemeSelected =
localStorage.getItem("darkSwitch") !== null &&
localStorage.getItem("darkSwitch") === "dark";
darkThemeSelected
? document.body.setAttribute("data-theme", "dark")
: document.body.removeAttribute("data-theme");
if (darkThemeSelected){
document.getElementById("darkModeText").innerHTML=`Turn Lights On`;
document.getElementById("darkModeIcon").className="zmdi zmdi-brightness-5";
}
else {
document.getElementById("darkModeText").innerHTML=`Turn Lights Off`;
document.getElementById("darkModeIcon").className="zmdi zmdi-brightness-3";
}
}
正如您所看到的,我正在修改一些尚不存在的元素,因为它们存在于正文中。如何在加载之前实现深色主题?我正在使用 node.js 和 express。
【问题讨论】:
-
这还有用吗?在 CSS 中,如果最后一个规则具有相同的特异性,则它们获胜。假设您的 Express 服务器也提供 HTML,您需要相应地切换
<link rel="stylesheet" />标记。为了更好地工作,将主题的偏好(浅色/深色)存储在客户端的 cookie 中并使用 Express 读取它(cookie 总是随请求发送)。 -
有什么好处? localStorage 不是 cookie 吗?
-
不,localStorage 保留在客户端上(但在浏览器关闭后仍然存在 - 与 sessionStorage 不同)。
标签: javascript html css node.js express