自定义的 JavaScript 按钮,用于更改页面背景和文字颜色。

huangapple go评论66阅读模式
英文:

Custom javascript button to change body background and font color

问题

以下是翻译好的部分:

注意:不是重复的问题,因为这是自定义的代码,没有其他答案可以解决我的问题,而且这只涉及JavaScript,不基于jQuery。

在阅读了数百篇其他帖子和答案后,我决定创建一个简单的函数,而不是使用复杂/高级的方法,只使用JavaScript来实现。问题是,我昨天刚开始学习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 自定义的 JavaScript 按钮,用于更改页面背景和文字颜色。

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

&lt;button class=&quot;change&quot; value=&quot;on&quot;&gt;Dark mode is on&lt;/a&gt;
&lt;button class=&quot;change&quot; value=&quot;off&quot;&gt;Dark mode is off&lt;/a&gt;
#would be nice with a single toggle button

Javascript

var document.querySelector(&quot;.change&quot;)
var body = document.getElementsByTagName(&quot;body&quot;)

btn.addEventListener(&quot;click&quot;, (e) =&gt; {
if (body[0].style.backgroundColor === &quot;on&quot;) {
body[0].style.backgroundColor = &quot;#101010&quot;
body[0].style.color = &quot;#FAFAFA&quot;

} else {
body[0].style.backgroundColor = &quot;#FAFAFA&quot;
body[0].style.color = &quot;#101010&quot;

}
localStorage.setItem(&quot;bgColor&quot;, body[0].style.backgroundColor)
localStorage.setItem(&quot;textColor&quot;, body[0].style.color)
})
body[0].style.backgroundColor = localStorage.getItem(&quot;bgColor&quot;)
body[0].style.color = localStorage.getItem(&quot;textColor&quot;)

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
&lt;button id=&quot;toggleBtn&quot;&gt;Dark mode is on&lt;/button&gt;

javascript

var toggleBtn = document.getElementById(&quot;toggleBtn&quot;);
var body = document.getElementsByTagName(&quot;body&quot;)[0];

toggleBtn.addEventListener(&quot;click&quot;, function() {
  var bgColor, textColor;

  if (body.classList.contains(&quot;dark-mode&quot;)) {
    body.classList.remove(&quot;dark-mode&quot;);
    toggleBtn.textContent = &quot;Dark mode is on&quot;;
    bgColor = &quot;#FFFFFF&quot;;
    textColor = &quot;#101010&quot;;
  } else {
    body.classList.add(&quot;dark-mode&quot;);
    toggleBtn.textContent = &quot;Dark mode is off&quot;;
    bgColor = &quot;#101010&quot;;
    textColor = &quot;#FFFFFF&quot;;
  }

  body.style.backgroundColor = bgColor;
  body.style.color = textColor;

  // Save the chosen colors to localStorage
  localStorage.setItem(&quot;bgColor&quot;, bgColor);
  localStorage.setItem(&quot;textColor&quot;, textColor);
});

// Retrieve saved colors from localStorage
var savedBgColor = localStorage.getItem(&quot;bgColor&quot;);
var savedTextColor = localStorage.getItem(&quot;textColor&quot;);

if (savedBgColor &amp;&amp; savedTextColor) {
  body.style.backgroundColor = savedBgColor;
  body.style.color = savedTextColor;

  if (savedBgColor === &quot;#101010&quot; &amp;&amp; savedTextColor === &quot;#FFFFFF&quot;) {
    body.classList.add(&quot;dark-mode&quot;);
    toggleBtn.textContent = &quot;Dark mode is off&quot;;
  }
}
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: &quot;&quot;;
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(&#39;DOMContentLoaded&#39;, function() {
var checkbox = document.getElementById(&#39;change&#39;); //Get the status os the checkbox
checkbox.addEventListener(&#39;change&#39;, function() { //Checks if the status changed
if (checkbox.checked) {
document.body.classList.add(&#39;dark-theme&#39;); //Uses the dark-theme that is in the CSS file.
localStorage.setItem(&#39;theme&#39;, &#39;dark&#39;); //Saves the &quot;theme&quot; in local storage
} else {
document.body.classList.remove(&#39;dark-theme&#39;); //Goes back to normal model in the CSS file
localStorage.setItem(&#39;theme&#39;, &#39;light&#39;);  //Saves the &quot;theme&quot; in local storage
}
});
var savedTheme = localStorage.getItem(&#39;theme&#39;); //When you load the page, it will check which &#39;theme&#39; is stored. If it is normal, it does nothing, if it&#39;s dark, checks the checkbox and uses the dark-theme in CSS.
if (savedTheme === &#39;dark&#39;) {
checkbox.checked = true;
document.body.classList.add(&#39;dark-theme&#39;);
}
});

<!-- 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: &quot;&quot;;
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 -->

&lt;label for=&quot;change&quot;&gt;Dark mode&lt;/label&gt;
&lt;label class=&quot;switch&quot;&gt;
&lt;input type=&quot;checkbox&quot; id=&quot;change&quot;&gt;
&lt;span class=&quot;slider round&quot;&gt;&lt;/span&gt;
&lt;/label&gt;

<!-- 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 -->

&lt;label class=&quot;switch&quot;&gt;
&lt;input type=&quot;checkbox&quot;&gt;
&lt;span class=&quot;slider round&quot;&gt;&lt;/span&gt;
&lt;/label&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月19日 21:28:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76507109.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定