英文:
Custom javascript button to change body background and font color
问题
以下是翻译好的部分:
注意:不是重复的问题,因为这是自定义的代码,没有其他答案可以解决我的问题,而且这只涉及JavaScript,不基于jQuery。
在阅读了数百篇其他帖子和答案后,我决定创建一个简单的函数,而不是使用复杂/高级的方法,只使用JavaScript来实现。问题是,我昨天刚开始学习JavaScript
我想要的:
我希望用户可以点击一个单一的切换按钮来打开/关闭深色模式,而不需要在代码中复杂的操作,只需将值保存在localStorage中并/或从localStorage中检索所选择的值。
颜色只有2种:
深色模式是:页面背景 #101010 和页面字体颜色 #FFFFFF
浅色模式是:页面背景 #FFFFFF 和页面字体颜色 #101010
我拥有的:
HTML
<button class="change" value="on">深色模式已启用</button>
<button class="change" value="off">深色模式已关闭</button>
# 最好使用一个单一的切换按钮
JavaScript
var btn = document.querySelector(".change");
var body = document.getElementsByTagName("body");
btn.addEventListener("click", (e) => {
if (body[0].style.backgroundColor === "on") {
body[0].style.backgroundColor = "#101010";
body[0].style.color = "#FAFAFA";
} else {
body[0].style.backgroundColor = "#FAFAFA";
body[0].style.color = "#101010";
}
localStorage.setItem("bgColor", body[0].style.backgroundColor);
localStorage.setItem("textColor", body[0].style.color);
});
body[0].style.backgroundColor = localStorage.getItem("bgColor");
body[0].style.color = localStorage.getItem("textColor");
我需要的是:
使它可以保存用户选择的颜色,并使用localStorage记住它们。
知道是否可能使按钮的文本切换为“深色模式已启用/深色模式已关闭”。
我使用这段代码创建了一个Codepen,你可以在其中查看代码:https://codepen.io/familias/pen/gOQMrjX
英文:
Note: not a duplicated because this is a custom made code and no other answer is answering my question, also because this is only javascript and not Jquery based
After reading hundreds of other posts and answers I decided to create a simple function instead of complicated/advanced ones, using just javascript. The problem is that I just started to learn javascript yesterday
What I want:
I want users to click on a single toggle button to turn dark mode on/off without complications in the code but just to save the value in localStorage and/or retrieve the chosen values from localStorage
The colors are just 2:
Dark mode is: body background #101010 and body font color if #FFFFFF
Light mode is: body background #FFFFFF and body font color if #101010
What I have
HTML
<button class="change" value="on">Dark mode is on</a>
<button class="change" value="off">Dark mode is off</a>
#would be nice with a single toggle button
Javascript
var document.querySelector(".change")
var body = document.getElementsByTagName("body")
btn.addEventListener("click", (e) => {
if (body[0].style.backgroundColor === "on") {
body[0].style.backgroundColor = "#101010"
body[0].style.color = "#FAFAFA"
} else {
body[0].style.backgroundColor = "#FAFAFA"
body[0].style.color = "#101010"
}
localStorage.setItem("bgColor", body[0].style.backgroundColor)
localStorage.setItem("textColor", body[0].style.color)
})
body[0].style.backgroundColor = localStorage.getItem("bgColor")
body[0].style.color = localStorage.getItem("textColor")
What I need
To make it save the chosen colors by the user and remember them with localstorage
To know if it is possible to make the button toggle with the text:
Dark mode is on/Dark mode is off
I created this Codepen with the code
https://codepen.io/familias/pen/gOQMrjX
答案1
得分: 0
这部分的内容已经翻译好了:
"这应该现在可以正常工作,我刚刚添加到你的代码中。
用户选择的(模式)颜色将保存在本地存储中,并在页面加载时检索。此外,我更新了CSS,包括一个.dark-mode
类,将暗模式样式应用于body
元素。
<button id="toggleBtn">Dark mode is on</button>
javascript
var toggleBtn = document.getElementById("toggleBtn");
var body = document.getElementsByTagName("body")[0];
toggleBtn.addEventListener("click", function() {
var bgColor, textColor;
if (body.classList.contains("dark-mode")) {
body.classList.remove("dark-mode");
toggleBtn.textContent = "Dark mode is on";
bgColor = "#FFFFFF";
textColor = "#101010";
} else {
body.classList.add("dark-mode");
toggleBtn.textContent = "Dark mode is off";
bgColor = "#101010";
textColor = "#FFFFFF";
}
body.style.backgroundColor = bgColor;
body.style.color = textColor;
// Save the chosen colors to localStorage
localStorage.setItem("bgColor", bgColor);
localStorage.setItem("textColor", textColor);
});
// Retrieve saved colors from localStorage
var savedBgColor = localStorage.getItem("bgColor");
var savedTextColor = localStorage.getItem("textColor");
if (savedBgColor && savedTextColor) {
body.style.backgroundColor = savedBgColor;
body.style.color = savedTextColor;
if (savedBgColor === "#101010" && savedTextColor === "#FFFFFF") {
body.classList.add("dark-mode");
toggleBtn.textContent = "Dark mode is off";
}
}
body {
margin: 0;
height: 100%;
}
.dark-mode {
background-color: #101010;
color: #FFFFFF;
}
"
英文:
This should work fine now, I just added on to your code.
The selected (mode)colors by the user will be saved in localStorage and retrieved when the page is loaded. Additionally I updated the CSS to include a .dark-mode
class that applies the dark mode styles to the body element.
html
<button id="toggleBtn">Dark mode is on</button>
javascript
var toggleBtn = document.getElementById("toggleBtn");
var body = document.getElementsByTagName("body")[0];
toggleBtn.addEventListener("click", function() {
var bgColor, textColor;
if (body.classList.contains("dark-mode")) {
body.classList.remove("dark-mode");
toggleBtn.textContent = "Dark mode is on";
bgColor = "#FFFFFF";
textColor = "#101010";
} else {
body.classList.add("dark-mode");
toggleBtn.textContent = "Dark mode is off";
bgColor = "#101010";
textColor = "#FFFFFF";
}
body.style.backgroundColor = bgColor;
body.style.color = textColor;
// Save the chosen colors to localStorage
localStorage.setItem("bgColor", bgColor);
localStorage.setItem("textColor", textColor);
});
// Retrieve saved colors from localStorage
var savedBgColor = localStorage.getItem("bgColor");
var savedTextColor = localStorage.getItem("textColor");
if (savedBgColor && savedTextColor) {
body.style.backgroundColor = savedBgColor;
body.style.color = savedTextColor;
if (savedBgColor === "#101010" && savedTextColor === "#FFFFFF") {
body.classList.add("dark-mode");
toggleBtn.textContent = "Dark mode is off";
}
}
css
body {
margin: 0;
height: 100%;
}
.dark-mode {
background-color: #101010;
color: #FFFFFF;
}
答案2
得分: 0
新手也在这里,但我会尽力帮助您。
我建议使用复选框。您可以使用CSS来创建一个滑块按钮。按照下面的说明和我的评论进行操作。
<!-- 开始代码片段:js 隐藏:false 控制台:true Babel:false -->
<!-- 语言:lang-css -->
/* 开关 - 滑块周围的框 */
.switch {
position: relative;
display: inline-block;
width: 60px;
height: 34px;
}
/* 隐藏默认的HTML复选框 */
.switch input {
opacity: 0;
width: 0;
height: 0;
}
/* 滑块 */
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
-webkit-transition: .4s;
transition: .4s;
}
.slider:before {
position: absolute;
content: "";
height: 26px;
width: 26px;
left: 4px;
bottom: 4px;
background-color: white;
-webkit-transition: .4s;
transition: .4s;
}
input:checked + .slider {
background-color: #2196F3;
}
input:focus + .slider {
box-shadow: 0 0 1px #2196F3;
}
<!-- 开始代码片段:js 隐藏:false 控制台:true Babel:false -->
<!-- 语言:lang-js -->
window.addEventListener('DOMContentLoaded', function() {
var checkbox = document.getElementById('change'); //获取复选框的状态
checkbox.addEventListener('change', function() { //检查状态是否改变
if (checkbox.checked) {
document.body.classList.add('dark-theme'); //使用CSS文件中的dark-theme
localStorage.setItem('theme', 'dark'); //将“theme”保存到本地存储中
} else {
document.body.classList.remove('dark-theme'); //返回到CSS文件中的正常模式
localStorage.setItem('theme', 'light'); //将“theme”保存到本地存储中
}
});
var savedTheme = localStorage.getItem('theme'); //当加载页面时,它将检查存储的“theme”。如果是正常的,它不会执行任何操作;如果是dark的,它会检查复选框并在CSS中使用dark-theme。
if (savedTheme === 'dark') {
checkbox.checked = true;
document.body.classList.add('dark-theme');
}
});
<!-- 语言:lang-css -->
/* 开关 - 滑块周围的框 */
.switch {
position: relative;
display: inline-block;
width: 60px;
height: 34px;
}
/* 隐藏默认的HTML复选框 */
.switch input {
opacity: 0;
width: 0;
height: 0;
}
/* 滑块 */
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
-webkit-transition: .4s;
transition: .4s;
}
.slider:before {
position: absolute;
content: "";
height: 26px;
width: 26px;
left: 4px;
bottom: 4px;
background-color: white;
-webkit-transition: .4s;
transition: .4s;
}
input:checked + .slider {
background-color: #2196F3;
}
input:focus + .slider {
box-shadow: 0 0 1px #2196F3;
}
input:checked + .slider:before {
-webkit-transform: translateX(26px);
-ms-transform: translateX(26px);
transform: translateX(26px);
}
/* 圆角滑块 */
.slider.round {
border-radius: 34px;
}
.slider.round:before {
border-radius: 50%;
}
/* 正常模式 */
body {
background-color: white;
color: black;
}
/* 深色模式 */
body.dark-theme {
background-color: black;
color: white;
}
<!-- 语言:lang-html -->
<label for="change">Dark mode</label>
<label class="switch">
<input type="checkbox" id="change">
<span class="slider round"></span>
</label>
<!-- 结束代码片段 -->
input:checked + .slider:before {
-webkit-transform: translateX(26px);
-ms-transform: translateX(26px);
transform: translateX(26px);
}
/* 圆角滑块 */
.slider.round {
border-radius: 34px;
}
.slider.round:before {
border-radius: 50%;
}
<!-- 语言:lang-html -->
<label class="switch">
<input type="checkbox">
<span class="slider round"></span>
</label>
<!-- 结束代码片段 -->
请注意,我只翻译了代码部分,不包括代码的注释。如果需要更多帮助,请随时提出。
英文:
Newb here also, buy I'll try to help you.
I suggest using a checkbox. You can use CSS to make a slider button. Follow below what you need with my comments.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
/* The switch - the box around the slider */
.switch {
position: relative;
display: inline-block;
width: 60px;
height: 34px;
}
/* Hide default HTML checkbox */
.switch input {
opacity: 0;
width: 0;
height: 0;
}
/* The slider */
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
-webkit-transition: .4s;
transition: .4s;
}
.slider:before {
position: absolute;
content: "";
height: 26px;
width: 26px;
left: 4px;
bottom: 4px;
background-color: white;
-webkit-transition: .4s;
transition: .4s;
}
input:checked + .slider {
background-color: #2196F3;
}
input:focus + .slider {
box-shadow: 0 0 1px #2196F3;
}
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
window.addEventListener('DOMContentLoaded', function() {
var checkbox = document.getElementById('change'); //Get the status os the checkbox
checkbox.addEventListener('change', function() { //Checks if the status changed
if (checkbox.checked) {
document.body.classList.add('dark-theme'); //Uses the dark-theme that is in the CSS file.
localStorage.setItem('theme', 'dark'); //Saves the "theme" in local storage
} else {
document.body.classList.remove('dark-theme'); //Goes back to normal model in the CSS file
localStorage.setItem('theme', 'light'); //Saves the "theme" in local storage
}
});
var savedTheme = localStorage.getItem('theme'); //When you load the page, it will check which 'theme' is stored. If it is normal, it does nothing, if it's dark, checks the checkbox and uses the dark-theme in CSS.
if (savedTheme === 'dark') {
checkbox.checked = true;
document.body.classList.add('dark-theme');
}
});
<!-- language: lang-css -->
/* The switch - the box around the slider */
.switch {
position: relative;
display: inline-block;
width: 60px;
height: 34px;
}
/* Hide default HTML checkbox */
.switch input {
opacity: 0;
width: 0;
height: 0;
}
/* The slider */
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
-webkit-transition: .4s;
transition: .4s;
}
.slider:before {
position: absolute;
content: "";
height: 26px;
width: 26px;
left: 4px;
bottom: 4px;
background-color: white;
-webkit-transition: .4s;
transition: .4s;
}
input:checked + .slider {
background-color: #2196F3;
}
input:focus + .slider {
box-shadow: 0 0 1px #2196F3;
}
input:checked + .slider:before {
-webkit-transform: translateX(26px);
-ms-transform: translateX(26px);
transform: translateX(26px);
}
/* Rounded sliders */
.slider.round {
border-radius: 34px;
}
.slider.round:before {
border-radius: 50%;
}
/* Normal Mode*/
body {
background-color: white;
color: black;
}
/* Dark mode */
body.dark-theme {
background-color: black;
color: white;
}
<!-- language: lang-html -->
<label for="change">Dark mode</label>
<label class="switch">
<input type="checkbox" id="change">
<span class="slider round"></span>
</label>
<!-- end snippet -->
input:checked + .slider:before {
-webkit-transform: translateX(26px);
-ms-transform: translateX(26px);
transform: translateX(26px);
}
/* Rounded sliders */
.slider.round {
border-radius: 34px;
}
.slider.round:before {
border-radius: 50%;
}
<!-- language: lang-html -->
<label class="switch">
<input type="checkbox">
<span class="slider round"></span>
</label>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论