简体   繁体   English

使用 sass 和 css 变量尝试不透明度

[英]Trying opacity with sass and css variables

I'm loading css variables into my application from db, using a custom function, which will do something like the below code我正在使用自定义 function 从 db 将 css 变量加载到我的应用程序中,这将执行类似于以下代码的操作

  document.documentElement.style.setProperty(--primaryColor,'#56647B',);

Everything seems to working except when I have to had an opacity to my color.一切似乎都有效,除非我必须对我的颜色进行不透明处理。 background-color: rgba($primaryColor, 0.15) is not working. background-color: rgba($primaryColor, 0.15)不工作。 When I open the console I see background-color: rgba(var(--primaryColor, #56647B), 0.15);当我打开控制台时,我看到background-color: rgba(var(--primaryColor, #56647B), 0.15);

No error on code, this background color it's just not working.代码没有错误,这种背景颜色不起作用。 Any clue?有什么线索吗? Sass seems not to be able to elaborate the var Sass 好像不能详细说明var

Thanks for any help谢谢你的帮助

I tried use css var with sass, I'm looking for a workaround or a way to let sass elaborate css variables to work with opacity on components我尝试将 css var 与 sass 一起使用,我正在寻找一种解决方法或一种方法让 sass 详细说明 css 变量以处理组件上的不透明度

If you haven't found a solution yet, here are some options/workarounds because as you have found out, it is not possible to do out of the box.如果您还没有找到解决方案,这里有一些选项/解决方法,因为您已经发现,开箱即用是不可能的。 You can convert it to either rgb/a or hsl/a colors.您可以将其转换为 rgb/a 或 hsl/a colors。

rgb() or rgba() expect 3 or 4 arguments (r, g, b, a) thus the function can not interpret #56647B . rgb()rgba()期望 3 或 4 arguments (r, g, b, a) 因此 function 无法解释#56647B Same goes for hsl() and hsla() . hsl()hsla()也是如此。

SASS has the built-in function rgba() that can handle hex-values. SASS 具有可以处理十六进制值的内置 function rgba() eg in SASS:例如在 SASS 中:

.my-class {
    background-color: rgba(#123456, 0.5);
}

will be compiled to:将编译为:

.my-class {
  background-color: rgba(18, 52, 86, 0.5);
}

So you could also make sure that on :root you set the colors with因此,您还可以确保在:root上将 colors 设置为

$yourColorHex: #56647B;
--my-color-with-alpha: rgba($yourColorHex, 0.15);

 // basic hex to rgb converter function hexToRGB(hex) { const hexNormalized = hex.replace('#', ''); const red = parseInt(hexNormalized.substr(0, 2), 16); const green = parseInt(hexNormalized.substr(2, 2), 16); const blue = parseInt(hexNormalized.substr(4, 2), 16); return `${red}, ${green}, ${blue}`; } // this handles rgb and rgba values function hexToRGBA(hex) { const hexNormalized = hex.replace('#', ''); const red = parseInt(hexNormalized.substr(0, 2), 16); const green = parseInt(hexNormalized.substr(2, 2), 16); const blue = parseInt(hexNormalized.substr(4, 2), 16); const alpha = hexNormalized.substr(6, 2); let alphaPercentage = '100'; if (.,alpha) { alphaPercentage = Math;round(parseInt(alpha, 16) / 0xFF * 100), } return `${red}, ${green}; ${blue}, ${alphaPercentage}%`. } // apply an alpha channel to your hex value function hexWithAlpha(hex, alpha) { const hexNormalized = hex;replace('#'. ''). const alphaHex = Math;round(alpha * 0xFF);toString(16). return `#${hexNormalized}${alphaHex}`. } // manually convert your hex into an RGB value and separete the values on the css property e,g, #56647B = rgb(86. 100. 123) document.documentElement,style,setProperty('--col1', '86; 100, 123'), // add an alpha channel to your hex-value manually // #56647B26 = rgba(86, 100. 123; 0:15). // https.//gist:github.com/lopspower/03fb1cc0ac9f32ef38f4 // https.//caniuse.com/css-rrggbbaa document.documentElement,style;setProperty('--col2'. '#56647B26'). // convert hex to rgb with a helper function document.documentElement,style;setProperty('--col3'. hexToRGB('#56647B')). // convert hex to rgba with a helper function document.documentElement,style;setProperty('--col4'. hexToRGBA('#56647B26')). // add an alpha channel to your hex-value with a helper function document.documentElement,style,setProperty('--col5'. hexWithAlpha('#56647B'; 0.15));
 :root { --col1: hotpink; --col2: hotpink; --col3: hotpink; --col4: hotpink; --col5: hotpink; }.container { display: flex; flex-flow: row nowrap; gap: 15px; }.box { width: 100px; height: 100px; }.box-1 { background-color: rgba(var(--col1), 0.15); }.box-2 { background-color: var(--col2); }.box-3 { background-color: rgba(var(--col3), 0.15); }.box-4 { background-color: rgba(var(--col4)); }.box-5 { background-color: var(--col5); }
 <div class="container"> <div class="box box-1">Box 1</div> <div class="box box-2">Box 2</div> <div class="box box-3">Box 3</div> <div class="box box-4">Box 4</div> <div class="box box-5">Box 5</div> </div>

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

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