如何在JavaScript中渲染CSS样式?

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

How to render CSS styling in JavaScript?

问题

I am working on a YouTube website redesign as a hobby, and I was working on a light/dark mode system. I got the variable changing system to work, but I don't know how to render the changes I made.

Here's my HTML file (only the body part, click here for full view):

<div class="header">
  <a href="https://codepen.io/ReallyBadDeveloper/pen/mdzKqZY">
  <img src="https://www.youtube.com/s/desktop/c1d331ff/img/favicon_48x48.png">
  </a>
  <button class="lightdark" onclick="darkLight">
    <svg viewBox="0 0 512 512" width="20" title="adjust" onclick="darkLight()">
      <path d="M8 256c0 136.966 111.033 248 248 248s248-111.034 248-248S392.966 8 256 8 8 119.033 8 256zm248 184V72c101.705 0 184 82.311 184 184 0 101.705-82.311 184-184 184z" />
    </svg>
    <p>Adjust colors</p>
  </button>
</div>
<div class="body">
  <p>cool</p>
</div>

JavaScript code:

var r = document.querySelector(':root');
var isdark = 0;

function darkLight() {
  if (isdark == 0) {
    r.style.setProperty('--backgroundcolor', '#1a1a1a');
    r.style.setProperty('--bodycolor', '#3b3b3b')
    r.style.setProperty('--fontcolor', 'white')
    isdark = 1;
  } else if (isdark == 1) {
    r.style.setProperty('--backgroundcolor', '#d4d4d4')
    r.style.setProperty('--bodycolor', '#f2f2f2')
    r.style.setProperty('--fontcolor', 'black')
    isdark = 0;
  }
}

CSS code:

@import url('https://fonts.googleapis.com/css2?family=Lexend&amp;family=Nunito&amp;display=swap');
:root {
  --maincolor: #ed1818;
  --backgroundcolor: #d4d4d4;
  --bodycolor: #f2f2f2;
  --fontcolor: black;
}
body {
  background-color: var(--backgroundcolor);
  font-family: Nunito, sans-serif;
  color: var(--fontcolor);
  padding: 20px;
}
.body {
  background-color: var(--bodycolor);
  border-radius: 5px;
  padding: 10px 30px;
}
.header {
  padding: 20px 0px;
}
button {
  border: 0px solid black;
  border-radius: 5px;
  background-color: var(--backgroundcolor);
  font-famliy: Nunito, sans-serif;
  color: var(--fontcolor);
  padding: 10px;
  transition-duration: 0.2s;
}
button:hover {
  border: 0px solid black;
  border-radius: 5px;
  background-color: var(--bodycolor);
  font-famliy: Nunito, sans-serif;
  color: var(--fontcolor);
  cursor: pointer;
}
.header {
  /* empty for now? */
}

Can somebody help?

英文:

I am working on a YouTube website redesign as a hobby, and I was working on a light/dark mode system. I got the variable changing system to work, but I don't know how to render the changes I made.
Here's my HTML file (only the body part, click here for full view):

&lt;div class=&quot;header&quot;&gt;
  &lt;a href=&quot;https://codepen.io/ReallyBadDeveloper/pen/mdzKqZY&quot;&gt;
  &lt;img src=&quot;https://www.youtube.com/s/desktop/c1d331ff/img/favicon_48x48.png&quot;&gt;
  &lt;/a&gt;
  &lt;button class=&quot;lightdark&quot; onclick=&quot;darkLight&quot;&gt;
    &lt;svg viewBox=&quot;0 0 512 512&quot; width=&quot;20&quot; title=&quot;adjust&quot; onclick=&quot;darkLight()&quot;&gt;
  &lt;path d=&quot;M8 256c0 136.966 111.033 248 248 248s248-111.034 248-248S392.966 8 256 8 8 119.033 8 256zm248 184V72c101.705 0 184 82.311 184 184 0 101.705-82.311 184-184 184z&quot; /&gt;
&lt;/svg&gt;
    &lt;p&gt;Adjust colors&lt;/p&gt;
  &lt;/button&gt;
&lt;/div&gt;
&lt;div class=&quot;body&quot;&gt;
  &lt;p&gt;cool&lt;/p&gt;
&lt;/div&gt;

JavaScript code:

var r = document.querySelector(&#39;:root&#39;);
var isdark = 0;

function darkLight() {
  if (isdark == 0) {
    r.style.setProperty(&#39;--backgroundcolor&#39;, &#39;#1a1a1a&#39;);
    r.style.setProperty(&#39;--bodycolor&#39;, &#39;#3b3b3b&#39;)
    r.style.setProperty(&#39;--fontcolor&#39;, &#39;white&#39;)
    isdark = 1;
  } else if (isdark == 1) {
    r.style.setProperty(&#39;--backgroundcolor&#39;, &#39;#d4d4d4&#39;)
    r.style.setProperty(&#39;--bodycolor&#39;, &#39;#f2f2f2&#39;)
    r.style.setProperty(&#39;--fontcolor&#39;, &#39;black&#39;)
    isdark = 0;
  }
}

CSS code:

@import url(&#39;https://fonts.googleapis.com/css2?family=Lexend&amp;family=Nunito&amp;display=swap&#39;);
:root {
  --maincolor: #ed1818;
  --backgroundcolor: #d4d4d4;
  --bodycolor: #f2f2f2;
  --fontcolor: black;
}
body {
  background-color: var(--backgroundcolor);
  font-family: Nunito, sans-serif;
  color: var(--fontcolor);
  padding: 20px;
}
.body {
  background-color: var(--bodycolor);
  border-radius: 5px;
  padding: 10px 30px;
}
.header {
  padding: 20px 0px;
}
button {
  border: 0px solid black;
  border-radius: 5px;
  background-color: var(--backgroundcolor);
  font-famliy: Nunito, sans-serif;
  color: var(--fontcolor);
  padding: 10px;
  transition-duration: 0.2s;
}
button:hover {
  border: 0px solid black;
  border-radius: 5px;
  background-color: var(--bodycolor);
  font-famliy: Nunito, sans-serif;
  color: var(--fontcolor);
  cursor: pointer;
}
.header {
  /* empty for now? */
}

Can somebody help?

答案1

得分: 1

这段代码的问题在于ifelse if块中的条件语句。

应将赋值运算符(=)更改为比较运算符(==或===)来检查isdark的值。

请将 = 运算符更改为 == 或 === 以正确比较 isdark 的值:

var r = document.querySelector(':root');
var isdark = 0;

function darkLight() {
  if (isdark == 0) {
    r.style.setProperty('--backgroundcolor', '#1a1a1a');
    r.style.setProperty('--bodycolor', '#3b3b3b');
    r.style.setProperty('--fontcolor', 'white');
    isdark = 1;
  } else if (isdark == 1) {
    r.style.setProperty('--backgroundcolor', '#d4d4d4');
    r.style.setProperty('--bodycolor', '#f2f2f2');
    r.style.setProperty('--fontcolor', 'black');
    isdark = 0;
  }
}
英文:

The issue with this code is in the conditional statements within the if and else if blocks.

Instead of using a comparison operator (== or ===) to check the value of isdark, the assignment operator (=) is being used.

This means that isdark is being set to 0 each time the if or else if block is executed, and the code inside the blocks will always run as if isdark was 0.

To fix this, change the = operator to == or === to properly compare the value of isdark:

var r = document.querySelector(&#39;:root&#39;);
var isdark = 0;

function darkLight() {
  if (isdark == 0) {
    r.style.setProperty(&#39;--backgroundcolor&#39;, &#39;#1a1a1a&#39;);
    r.style.setProperty(&#39;--bodycolor&#39;, &#39;#3b3b3b&#39;)
    r.style.setProperty(&#39;--fontcolor&#39;, &#39;white&#39;)
    isdark = 1;
  } else if (isdark == 1) {
    r.style.setProperty(&#39;--backgroundcolor&#39;, &#39;#d4d4d4&#39;)
    r.style.setProperty(&#39;--bodycolor&#39;, &#39;#f2f2f2&#39;)
    r.style.setProperty(&#39;--fontcolor&#39;, &#39;black&#39;)
    isdark = 0;
  }
}

huangapple
  • 本文由 发表于 2023年5月14日 01:20:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76244046.html
匿名

发表评论

匿名网友

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

确定