Issue
I was working on a YouTube website redesign, and I was working on a light/dark mode system. I set up some code, but I wasn't able to get it working. After some bugfixing, I got it working (with Stack Overflow help), but the button stops working after several clicks. Can somebody help?
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;
}
}
@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;
}
<div class="header">
<img src="https://www.youtube.com/s/desktop/c1d331ff/img/favicon_48x48.png">
<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>Lorem ipsum</p>
</div>
Solution
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;
}
}
Answered By - Ayman Elmimouni
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.