英文:
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("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"
}
}
<!-- language: lang-css -->
.body{
width: 100%;
height: 100vh;
min-height: 100vh;
background: #222327;
}
<!-- language: lang-html -->
<div class="box">
<button onclick="changeBgC()" class="menuButton">≣</button>
</div>
<!-- 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('.body').style.backgroundColor = "#29fd53"
console.log(i);
i+=1
}else{
document.querySelector('.body').style.backgroundColor = "#222327"
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('.body').style.backgroundColor = "#29fd53"
console.log(i);
i+=1
}else{
document.querySelector('.body').style.backgroundColor = "#222327"
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 -->
<div class="box">
<button onclick="bkg.change();" class="menuButton">≣</button>
</div>
<!-- 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('--bg-color').trim();
if(bgColor == "#222327"){
document.body.style.setProperty('--bg-color', '#29fd53');
} else {
document.body.style.setProperty('--bg-color', '#222327');
}
}
<!-- language: lang-css -->
body{
--bg-color: #222327;
width: 100%;
height: 100vh;
min-height: 100vh;
background-color: var(--bg-color);
}
<!-- language: lang-html -->
<div class="box">
<button onclick="changeBgC()" class="menuButton">≣</button>
</div>
<!-- end snippet -->
答案4
得分: 1
不应使用JavaScript直接更改元素的样式来切换主题。
然而,切换主题的更好解决方案是使用CSS变量而不是直接使用JavaScript更改元素的样式。
这种方法有多个好处,包括更好的性能、更容易维护和更灵活。
以下是如何使用CSS变量来在浅色和深色主题之间切换的示例:
- 使用CSS变量进行颜色更改:
.theme-light {
--bg-primary: #29fd53;
}
.theme-dark {
--bg-primary: #222327;
}
body {
background: var(--bg-primary);
}
- 切换主题:
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);
}
- HTML片段:
<div class="box">
<button onclick="switchTheme()" class="menuButton">≣</button>
</div>
- 不要忘记为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:
- Use CSS variables for color changes:
.theme-light {
--bg-primary: #29fd53;
}
.theme-dark {
--bg-primary: #222327;
}
body {
background: var(--bg-primary);
}
- Switching between themes:
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) // removes all theme classes, including the current one
document.body.classList.add(nextTheme)
}
- HTML snippet:
<div class="box">
<button onclick="switchTheme()" class="menuButton">≣</button>
</div>
- Don't forget to add the initial class for the body with one of the themes:
<body class="theme-light">
<body class="theme-dark">
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论