简体   繁体   English

使用JavaScript更改CSS样式颜色值

[英]Change CSS-Style Color Value with JavaScript

I have a box with a style that sets the color of the box to var(--box-color) . 我有一个样式的框,将框的颜色设置为var(--box-color) This variable is defined in a separate CSS file, which I use for constants etc.: 这个变量是在一个单独的CSS文件中定义的,我用它来表示常量等:

:root{
--box-color: rgba(250,250,250,1.0);
}

I want to make the color of the box lighter using JavaScript. 我希望使用JavaScript使盒子的颜色变浅。 I could just apply a brightness filter using box.style.filter = brightness(...) , but that doesn't seem very elegant, and as the box also has children that should not be affected by the dimming, it is not the way to go for me. 我可以使用box.style.filter = brightness(...)来应用亮度滤镜,但这看起来并不优雅,并且因为盒子也有不应该受到调光影响的孩子,所以它不是方式去找我。 Is there a way to change the specific color values using JavaScript, with variable initial values? 有没有办法使用JavaScript更改特定的颜色值,具有可变的初始值? Something like: 就像是:

box.style.backgroundColor.r -= 10;
box.style.backgroundColor.g -= 10;
box.style.backgroundColor.b -= 10;

I could also do it with String separation, if the color would be defined by an rgba(r,g,b,a) statement, but it is defined with a variable, and I also don't know how to get the value of that variable with JS. 如果颜色由rgba(r,g,b,a)语句定义,我也可以使用字符串分隔,但它是用变量定义的,我也不知道如何获取值JS的变量。

And if you don't understand my problem, feel free to ask in the comment section. 如果您不理解我的问题,请随时在评论部分询问。


I'm going for a less complicated and more elegant structure now, so this question does not require an answer anymore. 我现在要求一个不太复杂和更优雅的结构,所以这个问题不再需要答案了。 I'll not delete it though, in case other people come up with a similar problem. 我不会删除它,以防其他人提出类似的问题。

You can get the color value with javascript and split it into r , g , b and a colors for example like that: 你可以使用JavaScript,使色彩值,并将其分成r gba颜色,例如这样的:

let rgba = box.style.backgroundColor.match(/[\d\.]{1,3}/g);
// r === rgba[0]
// g === rgba[1]
// b === rgba[2]
// a === rgba[3]

then you can simply edit the rgba() values and convert them back to the original string: 然后你可以简单地编辑rgba()值并将它们转换回原始字符串:

rgba[3] -= 0.1

box.style.backgroundColor = 'rgba(' + rgba.join(',') + ')';

I just hope this can help somehow. 我只是希望这能以某种方式提供帮助。 Please tell me, if I don't understand you right. 请告诉我,如果我不理解你的话。


I'm sorry for the bad Question I wrote before - I still can't write comments :( 我很抱歉我之前写过的不好的问题 - 我仍然不能写评论:(

CSS Variables are seen as normal CSS properties, so you can just access them normally via element.style , eg document.documentElement.style['--variableName'] . CSS变量被视为普通的CSS属性,因此您可以通过element.style正常访问它们,例如document.documentElement.style['--variableName']

Note the usage of document.documentElement as the element we use to grab the styles - you used :root to define the variable, so we have to look for the values at the root element, which in, DOM, is the document. 注意使用document.documentElement作为我们用来获取样式的元素 - 你使用:root来定义变量,所以我们必须在根元素中查找值,DOM中的值是文档。

If you want to set the value of a CSS variable via JS, you can use document.documentElement.style.setProperty('--variableName', value) . 如果要通过JS设置CSS变量的值,可以使用document.documentElement.style.setProperty('--variableName', value)

In this case, you could just add some different CSS Variables that would store the new color value or just edit the existing one to suit your current needs. 在这种情况下,您可以添加一些不同的CSS变量来存储新的颜色值,或者只是编辑现有的变量来满足您当前的需求。

Useful links: 有用的链接:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM