【发布时间】:2019-12-19 15:00:02
【问题描述】:
我使用本地存储为网站构建了相当繁重的明暗模式。 从一个切换到另一个是无缝的,两者看起来都很棒。 然而,我在导航暗模式时遇到了闪烁问题。
两个样式表都以这个顺序在我的 header.php 中向上
<link id="light" rel="stylesheet" type="text/css" title="day" href="https://example.com/public/css/main.css">
<link id="dark" rel="stylesheet" type="text/css" title="night" href="https://example.com/public/css/night.css">
本地存储开关在同一个文件中使用 javascript 向下处理。
尝试了一百万件事后,原来是当我注释掉上面提到的第一个样式表时,闪烁消失了,浏览是无缝的。
所以我的问题是:如何使用将添加到 localstorage 部分的一些 javascript 命令注释掉样式表的第一行?还是有其他方法可以动态注释掉该样式表?
这里是我需要的本地存储功能:
<script>
document.getElementById("light").href = localStorage.getItem('light');
document.getElementById("dark").href = localStorage.getItem('dark');
function dayoff() {
document.getElementById("light").href = 'https://example.com/public/css/night.css';
localStorage.setItem( 'light', 'https://example.com/public/css/night.css' );
document.getElementById("light").href = 'https://example.com/public/css/night.css';
localStorage.setItem( 'dark', 'https://example.com/public/css/night.css' );
document.getElementById("logo").src = 'https://example.com/public/img/toplogo-night.png';
location.reload();
}
</script>
【问题讨论】:
-
我会在服务器端这样做。默认情况下,用户有一些样式,而另一种甚至不存在于 HTML 标记中。用户选择另一种样式并从 PHP 中保留一个 cookie(数据库字段会更好)并更改 CSS。
-
你能展示你的完整功能吗?
-
没问题。
标签: javascript css