英文:
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&family=Nunito&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):
<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&family=Nunito&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?
答案1
得分: 1
这段代码的问题在于if
和else 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(':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;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论