简体   繁体   English

背景颜色不会改变javascript

[英]Background color won't change javascript

I'm working on a little project And my background color won't change. 我正在做一个小项目,我的背景色不会改变。 https://codepen.io/JorisMertz/pen/gjmZOa https://codepen.io/JorisMertz/pen/gjmZOa

This is my Codepen, If you could help me out that would be really appreciated. 这是我的Codepen,如果您能帮助我,将不胜感激。

Or here is my code: 或者这是我的代码:

 var container = document.getElementById("container"); function togl() { if (container.style.background == "#232323") { container.style.background = "#fafafa"; } else { container.style.background = "#232323"; } } 
 * { font-family: roboto; } #container { color: #fafafa; background: #232323; padding: 20px; max-width: 60%; border-radius: 20px; top: 50%; left: 50%; box-shadow: 0px 0px 50px 0px rgba(0, 0, 0, 0.2); position: absolute; transform: translate(-50%, -50%); } body, html { padding: 0; margin: 0; width: 100%; height: 100%; background: #232323; } #toggle { appearance: none; border-style: none; padding: 7px 25px 7px 25px; position: relative; left: 50%; transform: translate(-50%); outline: none; cursor: pointer; } 
 <div id="container"> <h1>A cool heading</h1> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla faucibus interdum dapibus. Morbi est velit, aliquam ac eros in, aliquet semper tortor. Proin hendrerit ultricies dignissim. Proin suscipit sapien ac eleifend dignissim. Vestibulum sit amet rutrum mauris. Donec tincidunt sem at sapien auctor, quis rutrum augue luctus. Vivamus elementum non lacus non blandit.</p> <button id="toggle" onclick="togl()">Toggle</button> </div> 

The most recommended way is to toggle a class, and use addEventListener instead of inline script 推荐的方法是切换一个类,并使用addEventListener代替内联脚本

It's much simpler to maintain, and you keep the styles in one place, script in one and markup in one. 这很容易维护,您可以将样式放在一个位置,将脚本放在一个位置,将标记放在一个位置。

Stack snippet 堆栈片段

 var container = document.getElementById("container"); var button = document.getElementById("toggle"); button.addEventListener('click', function(){ container.classList.toggle('toggled'); }) 
 * { font-family: roboto; } #container { color: #fafafa; background: #232323; padding: 20px; max-width: 60%; border-radius: 20px; top: 50%; left: 50%; box-shadow: 0px 0px 50px 0px rgba(0, 0, 0, 0.2); position: absolute; transform: translate(-50%, -50%); } #container.toggled { background: #fafafa; color: #232323; } body, html { padding: 0; margin: 0; width: 100%; height: 100%; background: #232323; } #toggle { appearance: none; border-style: none; padding: 7px 25px 7px 25px; position: relative; left: 50%; transform: translate(-50%); outline: none; cursor: pointer; } 
 <div id="container"> <h1>A cool heading</h1> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla faucibus interdum dapibus. Morbi est velit, aliquam ac eros in, aliquet semper tortor.</p> <button id="toggle">Toggle</button> </div> 

Here is a linked to a fixed codepen with the correct code, along with some logging statements to show you the value. 这是带有正确代码的固定的链接,以及一些向您显示该值的日志记录语句。

In short, you need to get the value via getComputedStyle : 简而言之,您需要通过getComputedStyle获取值:

var container = document.getElementById("container");

function togl() {
  console.log(container.style.backgroundColor); // empty string
  var bcolor = window.getComputedStyle(container).backgroundColor;
  console.log(bcolor); // rgb(35, 35, 35)
  if (bcolor == "rgb(35, 35, 35)"){
    container.style.backgroundColor = "#fafafa";
  } else {
    container.style.backgroundColor = "#232323";
  }
}

Note that rgb(35, 35, 35) == #232323 , ie they are different ways of specifying the same color. 请注意, rgb(35, 35, 35) == #232323 ,即它们是指定相同颜色的不同方法。

Two things. 两件事情。 The first is, the element.style.background is checking the style property of the element (which is separate from the value supplied by the css. An assignment to said property is needed before it will have a value. You can see this in how the first time the function is run the value is empty. 首先是element.style.background正在检查element.style.background的style 属性 (该属性与css提供的值是分开的。需要对所述属性进行赋值,然后才能赋值)。第一次运行该函数时,该值为空。

The second issue is that the values returned are always going to be returned in rgb, not hex. 第二个问题是返回的值总是以rgb而不是十六进制返回。 Reversing the if statement to check for the first toggled state the code will work since the first check will be empty property value and thus not equal to the toggle color. 反转if语句以检查第一个切换状态,该代码将起作用,因为第一个检查将为空属性值,因此不等于切换颜色。

 var container = document.getElementById("container"); function togl() { console.log(container.style.background); if (container.style.background != "rgb(250, 250, 250)") { container.style.background = "#fafafa"; } else { container.style.background = "#232323"; } } 
 * { font-family: roboto; } #container { color: #fafafa; background: #232323; padding: 20px; max-width: 60%; border-radius: 20px; top: 50%; left: 50%; box-shadow: 0px 0px 50px 0px rgba(0, 0, 0, 0.2); position: absolute; transform: translate(-50%, -50%); } body, html { padding: 0; margin: 0; width: 100%; height: 100%; background: #232323; } #toggle { appearance: none; border-style: none; padding: 7px 25px 7px 25px; position: relative; left: 50%; transform: translate(-50%); outline: none; cursor: pointer; } 
 <div id="container"> <h1>A cool heading</h1> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla faucibus interdum dapibus. Morbi est velit, aliquam ac eros in, aliquet semper tortor. Proin hendrerit ultricies dignissim. Proin suscipit sapien ac eleifend dignissim. Vestibulum sit amet rutrum mauris. Donec tincidunt sem at sapien auctor, quis rutrum augue luctus. Vivamus elementum non lacus non blandit.</p> <button id="toggle" onclick="togl()">Toggle</button> </div> 

Alternatively, you could use the getComputedStyle method to determine the style of the element (which again, is different from its property value). 另外,您可以使用getComputedStyle方法确定元素的样式(这又不同于其属性值)。

 var container = document.getElementById("container"); function togl() { var curColor = window.getComputedStyle(container).backgroundColor;//always will report the value, even on the first execution console.log(curColor) if (curColor == "rgb(35, 35, 35)") { container.style.background = "#fafafa"; } else { container.style.background = "#232323"; } } 
 * { font-family: roboto; } #container { color: #fafafa; background: #232323; padding: 20px; max-width: 60%; border-radius: 20px; top: 50%; left: 50%; box-shadow: 0px 0px 50px 0px rgba(0, 0, 0, 0.2); position: absolute; transform: translate(-50%, -50%); } body, html { padding: 0; margin: 0; width: 100%; height: 100%; background: #232323; } #toggle { appearance: none; border-style: none; padding: 7px 25px 7px 25px; position: relative; left: 50%; transform: translate(-50%); outline: none; cursor: pointer; } 
 <div id="container"> <h1>A cool heading</h1> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla faucibus interdum dapibus. Morbi est velit, aliquam ac eros in, aliquet semper tortor. Proin hendrerit ultricies dignissim. Proin suscipit sapien ac eleifend dignissim. Vestibulum sit amet rutrum mauris. Donec tincidunt sem at sapien auctor, quis rutrum augue luctus. Vivamus elementum non lacus non blandit.</p> <button id="toggle" onclick="togl()">Toggle</button> </div> 

Finally, my preferred method, you could track the state in a separate flag. 最后,我的首选方法,您可以在单独的标志中跟踪状态。 The benefit of this is that you don't need to worry about any browsers reporting the color value differently from one another (eg if one browser doesn't use space characters in their rgb value, the previous code would break). 这样做的好处是,您无需担心任何浏览器报告的颜色值彼此不同(例如,如果一个浏览器在其rgb值中未使用空格字符,则先前的代码会中断)。

 var container = document.getElementById("container"); var toggled = false; function togl() { if (!toggled) { container.style.background = "#fafafa"; } else { container.style.background = "#232323"; } toggled = !toggled; } 
 * { font-family: roboto; } #container { color: #fafafa; background: #232323; padding: 20px; max-width: 60%; border-radius: 20px; top: 50%; left: 50%; box-shadow: 0px 0px 50px 0px rgba(0, 0, 0, 0.2); position: absolute; transform: translate(-50%, -50%); } body, html { padding: 0; margin: 0; width: 100%; height: 100%; background: #232323; } #toggle { appearance: none; border-style: none; padding: 7px 25px 7px 25px; position: relative; left: 50%; transform: translate(-50%); outline: none; cursor: pointer; } 
 <div id="container"> <h1>A cool heading</h1> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla faucibus interdum dapibus. Morbi est velit, aliquam ac eros in, aliquet semper tortor. Proin hendrerit ultricies dignissim. Proin suscipit sapien ac eleifend dignissim. Vestibulum sit amet rutrum mauris. Donec tincidunt sem at sapien auctor, quis rutrum augue luctus. Vivamus elementum non lacus non blandit.</p> <button id="toggle" onclick="togl()">Toggle</button> </div> 

Problem is solved now! 问题现在解决了! It was checking the style property at first, but i defined all my css in my seperate file. 起初它是在检查style属性,但是我在单独的文件中定义了所有的CSS。 Thanks for all the help! 感谢您的所有帮助!

The background color is empty on default, not #232323 because .style.background is only looking on the attributes of #container and not the css styles. 默认情况下,背景颜色为空,而不是#232323因为.style.background仅查看#container的属性,而不查看css样式。

 var container = document.getElementById("container"); function togl() { if (container.style.background == "") { container.style.background = "#fafafa"; } else { container.style.background = ""; } } 
 * { font-family: roboto; } #container { color: #fafafa; background: #232323; padding: 20px; max-width: 60%; border-radius: 20px; top: 50%; left: 50%; box-shadow: 0px 0px 50px 0px rgba(0, 0, 0, 0.2); position: absolute; transform: translate(-50%, -50%); } body, html { padding: 0; margin: 0; width: 100%; height: 100%; background: #232323; } #toggle { appearance: none; border-style: none; padding: 7px 25px 7px 25px; position: relative; left: 50%; transform: translate(-50%); outline: none; cursor: pointer; } 
 <div id="container"> <h1>A cool heading</h1> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla faucibus interdum dapibus. Morbi est velit, aliquam ac eros in, aliquet semper tortor. Proin hendrerit ultricies dignissim. Proin suscipit sapien ac eleifend dignissim. Vestibulum sit amet rutrum mauris. Donec tincidunt sem at sapien auctor, quis rutrum augue luctus. Vivamus elementum non lacus non blandit.</p> <button id="toggle" onclick="togl()">Toggle</button> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM