将深色模式/浅色模式保存到 localStoage
我有一个黑暗模式功能。 HTML、SCSS 和 javascript 工作正常。
但是当我刷新页面时,一切都恢复正常,而不是黑暗模式。
我想将深色模式和浅色模式的值保存到localStorage。但我很困惑,我不知道该怎么办。有人可以帮助我吗?请帮助我
,我已经搜索了几次,但仍然不起作用。如果您无法回答我的问题,请不要将此帖子设为类似帖子。因为我已经阅读并实现了它,但它不起作用。
这是我的 CODEPEN
模式切换器 HTML
<div id="modeSwitcher">
<input type="checkbox" class="checkbox" id="chk" />
<label class="label" for="chk">
<i class="fas fa-moon"></i>
<div class="ball"></div>
</label>
</div>
SCSS
#modeSwitcher{
margin: 5% 50%;
.checkbox {
opacity: 0;
position: absolute;
&:checked + {
.label {
.ball {
transform: translateX(35px);
&::after{
content: '';
position: absolute;
background-color: #0A0E27;
width: 13px;
height: 13px;
border-radius: 50%;
bottom: 50%;
left: -5%;
transform: translateY(50%);
}
}
}
}
}
.label {
background-color: #0A0E27;
border-radius: 50px;
cursor: pointer;
display: flex;
align-items: center;
justify-content: space-between;
padding: 5px;
margin: 0;
position: relative;
height: 16px;
width: 50px;
transform: scale(1.5);
.fa-moon{
color:#0A0E27 ;
}
.ball {
background-color: #FDC503;
border-radius: 50%;
position: absolute;
top: 3px;
left: 3px;
height: 20px;
width: 20px;
transform: translateX(0px);
transition: transform 0.2s linear;
}
}
}
body{
background-color: #fff;
&.dark{
background-color: black;
}
}
Javascript
const check = document.getElementById('chk');
check.addEventListener('change', () => {
document.body.classList.toggle('dark');
localStorage.darkMode=!localStorage.darkMode;
});
window.onload=function() {
if(localStorage.darkMode) document.body.classList.toggle('dark');
}
I have a dark mode feature. HTML and SCSS and javascript are working properly.
But when I refreshed the page, everything returned to normal, not in dark mode.
I want to save the values of dark mode and light mode to localStorage. but I'm confused, I don't know how. is there anyone can help me? Please help me
I've searched on several searches but it still doesn't work. If you can't answer my question, please don't make this post a similar post. because I've read and implemented it but it doesn't work.
this is my CODEPEN
Mode Switcher HTML
<div id="modeSwitcher">
<input type="checkbox" class="checkbox" id="chk" />
<label class="label" for="chk">
<i class="fas fa-moon"></i>
<div class="ball"></div>
</label>
</div>
SCSS
#modeSwitcher{
margin: 5% 50%;
.checkbox {
opacity: 0;
position: absolute;
&:checked + {
.label {
.ball {
transform: translateX(35px);
&::after{
content: '';
position: absolute;
background-color: #0A0E27;
width: 13px;
height: 13px;
border-radius: 50%;
bottom: 50%;
left: -5%;
transform: translateY(50%);
}
}
}
}
}
.label {
background-color: #0A0E27;
border-radius: 50px;
cursor: pointer;
display: flex;
align-items: center;
justify-content: space-between;
padding: 5px;
margin: 0;
position: relative;
height: 16px;
width: 50px;
transform: scale(1.5);
.fa-moon{
color:#0A0E27 ;
}
.ball {
background-color: #FDC503;
border-radius: 50%;
position: absolute;
top: 3px;
left: 3px;
height: 20px;
width: 20px;
transform: translateX(0px);
transition: transform 0.2s linear;
}
}
}
body{
background-color: #fff;
&.dark{
background-color: black;
}
}
Javascript
const check = document.getElementById('chk');
check.addEventListener('change', () => {
document.body.classList.toggle('dark');
localStorage.darkMode=!localStorage.darkMode;
});
window.onload=function() {
if(localStorage.darkMode) document.body.classList.toggle('dark');
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 css 中修改:
在 javascript 中修改:
Modify this in css:
Modify this in javascript:
您可以使用这一行
localStorage.setItem('darkMode', 'true');
存储数据,并且可以使用这一行检索
var darkMode = localStorage.getItem('darkMode');< /code>
然后用你自己的逻辑应用你的风格
You can store data with this line
localStorage.setItem('darkMode', 'true');
and you can retrieve with this one
var darkMode = localStorage.getItem('darkMode');
then just apply your style with your own logic