检查 JavaScript 中的 CSS 值

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

Check CSS value in javascript

问题

以下是您要翻译的内容:

我想让 JavaScript 检查我的 CSS 代码如果背景颜色 = #222327它将更改背景颜色为 #29fd53但如果旧的背景颜色 = #29fd53它将更改背景颜色为 #222327

我的 JavaScript 代码

function changeBgC() {
  var body = document.getElementsByTagName("body")[0];
  var computedStyle = window.getComputedStyle(body);
  var bodyBackgroundValue = computedStyle.getPropertyValue("background-color");
  console.log(bodyBackgroundValue);
  if (bodyBackgroundValue == "#222327") {
    document.querySelector('body').style.backgroundColor = "#29fd53";
  } else {
    document.querySelector('body').style.backgroundColor = "#222327";
  }
}

CSS 代码

.body {
  width: 100%;
  height: 100vh;
  min-height: 100vh;
  background: #222327;
}

HTML 代码

<div class="box">
  <button onclick="changeBgC()" class="menuButton"></button>
</div>

我只是尝试使用 i++(我在 HTML 代码上使用了 onclick),但它只会更改背景颜色为 #29fd53。当我再次按按钮时,它不会执行任何操作。

以下是您尝试的 JavaScript 代码:

function changeBgC() {
  var i = 0;
  if (i % 2 === 0) {
    document.querySelector('.body').style.backgroundColor = "#29fd53";
    console.log(i);
    i += 1;
  } else {
    document.querySelector('.body').style.backgroundColor = "#222327";
    i += 1;
  }
}
英文:

I want to make JavaScript check my css code and return me if background color = #222327 it will change background color to #29fd53 But if old background color = #29fd53 it will change background color to #222327

My Js code

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function changeBgC(){
var body = document.getElementsByTagName(&quot;body&quot;)[0];

var computedStyle = window.getComputedStyle(body)

var bodyBackgroundValue = computedStyle.getPropertyValue(&quot;background-color&quot;)
console.log(bodyBackgroundValue);
if (bodyBackgroundValue == &quot;#222327&quot;) {
    document.querySelector(&#39;body&#39;).style.backgroundColor = &quot;#29fd53&quot;
  } else {
    document.querySelector(&#39;body&#39;).style.backgroundColor = &quot;#222327&quot;
  }
}

<!-- language: lang-css -->

.body{
    width: 100%;
    height: 100vh;
    min-height: 100vh;
    background: #222327;
}

<!-- language: lang-html -->

&lt;div class=&quot;box&quot;&gt;
&lt;button onclick=&quot;changeBgC()&quot; class=&quot;menuButton&quot;&gt;≣&lt;/button&gt;
&lt;/div&gt;

<!-- end snippet -->

I just try to use i++ (I use onclick on my html code) but it only changes the background color to #29fd53. When I press the button again it doesn't do anything.

Here is what I try

function changeBgC(){
    var i = 0
    if (i % 2 === 0){
        document.querySelector(&#39;.body&#39;).style.backgroundColor = &quot;#29fd53&quot;
        console.log(i);
        i+=1
    }else{
        document.querySelector(&#39;.body&#39;).style.backgroundColor = &quot;#222327&quot;
        i+=1
    }
}

答案1

得分: 2

问题出在将变量 i 设置在函数作用域内。因此,每次调用 changeBgC() 都会将 i 设置为 0。

请在函数外声明 i,这样应该可以解决问题。

let i = 0;
function changeBgC(){
    if (i % 2 === 0){
        document.querySelector('.body').style.backgroundColor = "#29fd53";
        console.log(i);
        i += 1;
    }else{
        document.querySelector('.body').style.backgroundColor = "#222327";
        i += 1;
    }
}
英文:

Your problem lies in setting the i variable inside the function scope. So every time you call changeBgC() it will set i = 0.

Declare i outside the function and it should work

let i = 0;
function changeBgC(){
    if (i % 2 === 0){
        document.querySelector(&#39;.body&#39;).style.backgroundColor = &quot;#29fd53&quot;
        console.log(i);
        i+=1
    }else{
        document.querySelector(&#39;.body&#39;).style.backgroundColor = &quot;#222327&quot;
        i+=1
    }
}

答案2

得分: 1

这可以通过使用class而不是function来实现。这样i变量可以被封装起来。我使用了私有成员(#),因为它们在类的外部是不需要的。

class backg {
  #i = 0;
  #elem = document.body.style;

  change() {
    this.#i = !this.#i;
    this.#elem.backgroundColor = this.#i ? "#29fd53" : "#222327";
  }
}

const bkg = new backg();
.body {
  width: 100%;
  height: 100vh;
  min-height: 100vh;
  background: #222327;
}
<div class="box">
  <button onclick="bkg.change();" class="menuButton">≣</button>
</div>
英文:

This can be achieved using a class instead of a function. This way the i variable can be encapsulated. I have used private members (#) as they are not required ouside of the class.

<!-- begin snippet: js hide: false console: true babel: null -->

<!-- language: lang-js -->

class backg {

#i=0;
#elem=document.body.style;

change() {
this.#i=!this.#i;
this.#elem.backgroundColor = this.#i?"#29fd53":"#222327";
}
}

bkg=new backg();

<!-- language: lang-css -->

.body{
    width: 100%;
    height: 100vh;
    min-height: 100vh;
    background: #222327;
}

<!-- language: lang-html -->

&lt;div class=&quot;box&quot;&gt;
&lt;button onclick=&quot;bkg.change();&quot; class=&quot;menuButton&quot;&gt;≣&lt;/button&gt;
&lt;/div&gt;

<!-- end snippet -->

答案3

得分: 1

如@Teemu在上面提到的,getComputedStyle() 返回一个 rgb() 值,所以要获取HEX代码,您可以使用CSS变量而不是编写一个将RGB转换为HEX的函数。

trim函数用于去除尾随空格

function changeBgC(){
  let bgColor = getComputedStyle(document.body).getPropertyValue('--bg-color').trim();
  if(bgColor == "#222327"){
    document.body.style.setProperty('--bg-color', '#29fd53');
  } else {
    document.body.style.setProperty('--bg-color', '#222327');
  }
}
body {
  --bg-color: #222327;
  width: 100%;
  height: 100vh;
  min-height: 100vh;
  background-color: var(--bg-color);
}
<div class="box">
  <button onclick="changeBgC()" class="menuButton">≣</button>
</div>
英文:

as @Teemu mentioned above getComputedStyle() returns an rgb() value
so to get the HEX code you can use a css variable rather than writing a function to convert from RGB to HEX,

trim function used to remove trailing spaces

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function changeBgC(){
  let bgColor = getComputedStyle(document.body).getPropertyValue(&#39;--bg-color&#39;).trim();
  if(bgColor == &quot;#222327&quot;){
    document.body.style.setProperty(&#39;--bg-color&#39;, &#39;#29fd53&#39;);
  } else {
    document.body.style.setProperty(&#39;--bg-color&#39;, &#39;#222327&#39;);
  }
}

<!-- language: lang-css -->

body{
   --bg-color: #222327;
    width: 100%;
    height: 100vh;
    min-height: 100vh;
    background-color: var(--bg-color);
}

<!-- language: lang-html -->

&lt;div class=&quot;box&quot;&gt;
&lt;button onclick=&quot;changeBgC()&quot; class=&quot;menuButton&quot;&gt;≣&lt;/button&gt;
&lt;/div&gt;

<!-- end snippet -->

答案4

得分: 1

不应使用JavaScript直接更改元素的样式来切换主题。

然而,切换主题的更好解决方案是使用CSS变量而不是直接使用JavaScript更改元素的样式。

这种方法有多个好处,包括更好的性能、更容易维护和更灵活。

以下是如何使用CSS变量来在浅色和深色主题之间切换的示例:

  1. 使用CSS变量进行颜色更改:
.theme-light {
  --bg-primary: #29fd53;
}

.theme-dark {
  --bg-primary: #222327;
}

body {
  background: var(--bg-primary);
}
  1. 切换主题:
function switchTheme() {
    let themes = ['theme-light', 'theme-dark'];
    let currentTheme = [...document.body.classList].find(c => themes.includes(c)) ?? themes[0];
    let nextTheme = themes[themes.indexOf(currentTheme) + 1] ?? themes[0];
    document.body.classList.remove(...themes); // 移除所有主题类,包括当前的
    document.body.classList.add(nextTheme);
}
  1. HTML片段:
<div class="box">
  <button onclick="switchTheme()" class="menuButton">≣</button>
</div>
  1. 不要忘记为body添加初始类,其中包含其中一个主题:
<body class="theme-light">
<body class="theme-dark">
英文:

You should not use JavaScript to directly change the styles of elements for the purpose of switching themes.

However, a better solution for switching themes is to use CSS variables instead of directly changing the styles of elements with JavaScript.

This approach has several benefits, including better performance, easier maintenance, and more flexibility.
Here's an example of how you could use CSS variables to switch between light and dark themes:

  1. Use CSS variables for color changes:
.theme-light {
  --bg-primary: #29fd53;
}

.theme-dark {
  --bg-primary: #222327;
}

body {
  background: var(--bg-primary);
}
  1. Switching between themes:
function switchTheme() {
    let themes = [&#39;theme-light&#39;, &#39;theme-dark&#39;];
    let currentTheme = [...document.body.classList].find(c =&gt; themes.includes(c)) ?? themes[0]
    let nextTheme = themes[themes.indexOf(currentTheme)+1] ?? themes[0]
    document.body.classList.remove(...themes) // removes all theme classes, including the current one
    document.body.classList.add(nextTheme)
}
  1. HTML snippet:
&lt;div class=&quot;box&quot;&gt;
  &lt;button onclick=&quot;switchTheme()&quot; class=&quot;menuButton&quot;&gt;≣&lt;/button&gt;
&lt;/div&gt;
  1. Don't forget to add the initial class for the body with one of the themes:
&lt;body class=&quot;theme-light&quot;&gt;
&lt;body class=&quot;theme-dark&quot;&gt;

huangapple
  • 本文由 发表于 2023年3月31日 19:55:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75898254.html
匿名

发表评论

匿名网友

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

确定