【发布时间】:2020-01-07 12:28:57
【问题描述】:
我有一个暗/亮开关。我使用 cookie 让用户保持深色/浅色偏好。所以当他们再次访问时,他们会看到他们喜欢的主题。但是有一个烦人的问题。每次我刷新页面时,页面都会在短时间内显示浅色主题,然后再次呈现深色主题。如何解决这个问题?
const toggleSwitch = document.querySelector('.theme-switch input[type="checkbox"]');
function switchTheme(e) {
if (e.target.checked) {
document.documentElement.setAttribute('data-theme', 'dark');
localStorage.setItem('theme', 'dark'); //add this
}
else {
document.documentElement.setAttribute('data-theme', 'light');
localStorage.setItem('theme', 'light'); //add this
}
}
toggleSwitch.addEventListener('change', switchTheme, false);
const currentTheme = localStorage.getItem('theme') ? localStorage.getItem('theme') : null;
if (currentTheme) {
document.documentElement.setAttribute('data-theme', currentTheme);
if (currentTheme === 'dark') {
toggleSwitch.checked = true;
}
}
:root {
--bg-color: #fff;
--panel-bg: #fff;
--panel-bg-hover: #dcd8d8;
--panel-border: #22a7f0;
--panel-title-color: #22a7f0;
--panel-title-hover: #0072b1;
--panel-subtitle-color: #ffa500;
--panel-text-color: #000000;
--panel-button-bg: #dc4a4a;
--panel-button-hover-bg: #d83535;
--panel-button-text-color: #ffd7f8;
--panel-button-text-hover-color: #ffd7f8;
--input-bg: #e6e6e6;
--input-border-color: #ff0000;
--input-label-color: #ff0000;
--input-label-color-focus: #ff0000;
--input-text-color: #000000;
--search-input-bg: #fff;
--table-shorting: #22a7f0;
--table-text-color-1: #417d9e;
--table-text-color-2: #ff0000;
--table-text-color-3: #008000;
--table-text-color-4: #b17b17;
--table-text-color-5: rgb(87, 89, 146);
--table-options-color: #22a7f0;
--table-options-hover-color: #e6e6e6;
--dropdown-line-hover: #ddd;
--active-color: #87D37C;
--passive-color: #E74C3C;
}
[data-theme="dark"] {
--bg-color: #2f2f2f;
--panel-bg: #1e262c;
--panel-bg-hover: #2f3a42;
--panel-border: #ffa500;
--panel-title-color: #ffa500;
--panel-title-hover: #ffffffc2;
--panel-subtitle-color: #c3c6ce;
--panel-text-color: #ffffff;
--panel-button-bg: #dc4a4a;
--panel-button-hover-bg: #d83535;
--panel-button-text-color: #ffd7f8;
--panel-button-text-hover-color: #ffd7f8;
--input-bg: #1a2025;
--input-border-color: #3694ff;
--input-label-color: #cad1ff4d;
--input-label-color-focus: #3694ff;
--input-text-color: white;
--search-input-bg: #1e262c;
--table-shorting: #ffa500;
--table-text-color-1: #417d9e;
--table-text-color-2: #ff6262;
--table-text-color-3: #358e65;
--table-text-color-4: #3eafa4;
--table-text-color-5: #40c57b;
--table-options-color: #ffa500;
--table-options-hover-color: #ffa50042;
--dropdown-line-hover: #634747;
--active-color: #0f980f;
--passive-color: #bf1e0e;
}
body {
background-color: var(--bg-color);
}
.squares { display: inline-block; margin-top: 30px;}
.square1 {display: inline-block; width: 100px; height: 100px; background-color: var(--table-text-color-1); margin-right: 20px;}
.square2 {display: inline-block; width: 100px; height: 100px; background-color: var(--table-text-color-2); margin-right: 20px;}
.square3 {display: inline-block; width: 100px; height: 100px; background-color: var(--table-text-color-3); margin-right: 20px;}
.square4 {display: inline-block; width: 100px; height: 100px; background-color: var(--table-text-color-4); margin-right: 20px;}
.theme-switch-wrapper {
display: flex;
align-items: center;
position: -webkit-sticky;
position: sticky;
top: 10px;
}
.theme-switch {
display: inline-block;
height: 26px;
position: relative;
width: 52px;
}
.theme-switch input {
display:none;
}
.slider {
background-color: #ccc0;
border: 3px solid #a5a4a4;
bottom: 0;
cursor: pointer;
left: 0;
position: absolute;
right: 0;
top: 0;
transition: .4s;
}
.slider:before {
content: "";
position: absolute;
width: 16px;
height: 16px;
background-color: #a5a4a4;
bottom: 1.8px;
left: 3px;
transform: rotate(-45deg);
transition: .4s;
border-radius: 100%;
}
#darklightswitch:checked + .slider {
border: 3px solid #3694ff;
}
#darklightswitch:checked + .slider:before {
box-shadow: 5px 5px 0 0 #3694ff;
background: 0 0;
left: 21.1px;
}
.slider.round {
border-radius: 34px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<body>
<div class="theme-switch-wrapper">
<label class="theme-switch" for="darklightswitch">
<input type="checkbox" id="darklightswitch" />
<div class="slider round"></div>
</label>
</div>
<div class="squares">
<div class="square1"></div>
<div class="square2"></div>
<div class="square3"></div>
<div class="square4"></div>
</div>
</body>
我的来源:https://dev.to/ananyaneogi/create-a-dark-light-mode-switch-with-css-variables-34l8
笔:https://codepen.io/lastofdead/pen/dyPdPWY
编辑:
我的真实索引页;
<html>
<head>
<meta charset="UTF-8">
<meta name="robots" content="noindex, nofollow">
<!-- JS -->
<script src="js/sweetalert2.all.js"></script>
<script src="js/jquery-3.3.1.min.js" type="text/javascript"></script>
<script src="js/jquery.maskedinput.js?ver=1" type="text/javascript"></script>
<!-- CSS -->
<link href="css/darklight.css?ver=2.01" rel="stylesheet" type="text/css" />
<link href="css/style.css?ver=2.02" rel="stylesheet" type="text/css" />
<link href="css/print.css?ver=2.00" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="theme-switch-wrapper">
<label class="theme-switch" for="darklightswitch">
<input type="checkbox" id="darklightswitch" />
<div class="slider round"></div>
</label>
</div>
<!-- some divs -->
<script src="js/darklightswitch.js?ver=2.04" type="text/javascript"></script>
<script src="js/dropdown-responsive.js?ver=2.00"></script>
<script src='js/jquery.dataTables.min.js?ver=2.00' type="text/javascript"></script>
<script src="js/input.js" type="text/javascript"></script>
<script src="js/sweet-js.js?ver=2.00"></script>
</body>
</html>
编辑 2
这是最后一个。我仍然遇到同样的问题。
<html>
<head>
<meta charset="UTF-8">
<meta name="robots" content="noindex, nofollow">
<!-- JS -->
<script src="js/sweetalert2.all.js"></script>
<script src="js/jquery-3.3.1.min.js" type="text/javascript"></script>
<script src="js/jquery.maskedinput.js?ver=1" type="text/javascript"></script>
<script type="text/javascript">
const toggleSwitch = document.querySelector('.theme-switch input[type="checkbox"]');
function switchTheme(e) {
if (e.target.checked) {
document.documentElement.setAttribute('data-theme', 'dark');
localStorage.setItem('theme', 'dark'); //add this
}
else {
document.documentElement.setAttribute('data-theme', 'light');
localStorage.setItem('theme', 'light'); //add this
}
}
toggleSwitch.addEventListener('change', switchTheme, false);
/* çerezleme */
const currentTheme = localStorage.getItem('theme') ? localStorage.getItem('theme') : null;
if (currentTheme) {
document.documentElement.setAttribute('data-theme', currentTheme);
if (currentTheme === 'dark') {
toggleSwitch.checked = true;
}
}
</script>
<!-- CSS -->
<style>
:root {
--bg-color: #2f2f2f;
--panel-bg: #fff;
--panel-bg-hover: #dcd8d8;
--panel-border: #22a7f0;
--panel-title-color: #22a7f0;
--panel-title-hover: #0072b1;
--panel-subtitle-color: #ffa500;
--panel-text-color: #000000;
--panel-button-bg: #dc4a4a;
--panel-button-hover-bg: #d83535;
--panel-button-text-color: #ffd7f8;
--panel-button-text-hover-color: #ffd7f8;
--input-bg: #e6e6e6;
--input-border-color: #ff0000;
--input-label-color: #ff0000;
--input-label-color-focus: #ff0000;
--input-text-color: #000000;
--search-input-bg: #fff;
--table-shorting: #22a7f0;
--table-text-color-1: #417d9e;
--table-text-color-2: #ff0000;
--table-text-color-3: #008000;
--table-text-color-4: #b17b17;
--table-text-color-5: rgb(87, 89, 146);
--table-options-color: #22a7f0;
--table-options-hover-color: #e6e6e6;
--dropdown-line-hover: #ddd;
--active-color: #87D37C;
--passive-color: #E74C3C;
}
[data-theme="dark"] {
--bg-color: #2f2f2f;
--panel-bg: #1e262c;
--panel-bg-hover: #2f3a42;
--panel-border: #ffa500;
--panel-title-color: #ffa500;
--panel-title-hover: #ffffffc2;
--panel-subtitle-color: #c3c6ce;
--panel-text-color: #ffffff;
--panel-button-bg: #dc4a4a;
--panel-button-hover-bg: #d83535;
--panel-button-text-color: #ffd7f8;
--panel-button-text-hover-color: #ffd7f8;
--input-bg: #1a2025;
--input-border-color: #3694ff;
--input-label-color: #cad1ff4d;
--input-label-color-focus: #3694ff;
--input-text-color: white;
--search-input-bg: #1e262c;
--table-shorting: #ffa500;
--table-text-color-1: #417d9e;
--table-text-color-2: #ff6262;
--table-text-color-3: #358e65;
--table-text-color-4: #3eafa4;
--table-text-color-5: #40c57b;
--table-options-color: #ffa500;
--table-options-hover-color: #ffa50042;
--dropdown-line-hover: #634747;
--active-color: #0f980f;
--passive-color: #bf1e0e;
}
</style>
<link href="css/style.css?ver=2.02" rel="stylesheet" type="text/css" />
<link href="css/print.css?ver=2.00" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="theme-switch-wrapper">
<label class="theme-switch" for="darklightswitch">
<input type="checkbox" id="darklightswitch" />
<div class="slider round"></div>
</label>
</div>
<!-- some divs -->
<script src="js/dropdown-responsive.js?ver=2.00"></script>
<script src='js/jquery.dataTables.min.js?ver=2.00' type="text/javascript"></script>
<script src="js/input.js" type="text/javascript"></script>
<script src="js/sweet-js.js?ver=2.00"></script>
</body>
</html>
【问题讨论】:
-
这是因为您的
css在页面加载时首先应用,然后您的javascript加载然后转换为深色主题。 -
所以我只需要把css移到js吗?
-
如果你能从服务器端这样做会更好
-
服务器端;在将页面发送给用户之前,使用 php 在服务器上生成页面。所以将这部分
<script src="js/darklightswitch.js?ver=2.04" type="text/javascript"></script>移动到head标签中的<!-- JS -->部分下没有用?尝试添加内联代码,而不是链接到文档。 -
一个干净的解决方案是使用装载机。显示加载器,直到 JS 被加载,然后显示站点。
标签: javascript html css