简体   繁体   English

CSS变量和SCSS混合

[英]CSS variable & SCSS mixin

I need to be able to use CSS variables because I need to have an hover effect (background-color) to be customizable by my VueJs app. 我需要能够使用CSS变量,因为我需要具有悬停效果(背景色)才能由VueJs应用程序自定义。 But my CSS stylesheet should have a default value, which is stored in a nested SCSS map. 但是我的CSS样式表应具有默认值,该默认值存储在嵌套的SCSS映射中。 (map-getter is a function which returns values from nested maps) (map-getter是从嵌套地图返回值的函数)

I know that my SCSS code works, because I get the intended result when I do this: 我知道我的SCSS代码可以正常工作,因为这样做可以得到预期的结果:

.theme--dark .AppNavTile:hover {
  background-color: map-getter($theme-dark, AppNav, hover);
  //returns background-color: rgba(255, 255, 255, 0.87); in my browser's console
}

In order to use CSS variables, I can modify the code as follows: 为了使用CSS变量,我可以如下修改代码:

.theme--dark .AppNavTile:hover {
  --hover-bg-color: red;
  background-color: var(--hover-bg-color);
}

It works fine and I have a red background when hovering the element. 效果很好,将元素悬停时背景为红色。

Then I try to combine both: 然后,我尝试将两者结合起来:

.theme--dark .AppNavTile:hover {
  --hover-bg-color: map-getter($theme-dark, AppNav, hover);
  background-color: var(--hover-bg-color);
}

According to by browser's console, this returns the following: 根据浏览器的控制台,这将返回以下内容:

.theme--dark .AppNavTile:hover {
    --hover-bg-color: map-getter($theme-dark, AppNav, hover);
    background-color: var(--hover-bg-color);
}

So it seems that the SCSS code remains uncompiled in the CSS variable. 因此,似乎SCSS代码仍未编译在CSS变量中。 Is there any way around it? 有什么办法解决吗?

Thanks! 谢谢!

The "problem" with CSS variables is they can have any value – why map-getter($theme-dark, AppNav, hover) is rendered as is. CSS变量的“问题”在于它们可以具有任何值-为什么按原样呈现map-getter($ theme-dark,AppNav,hover)。 To instruct SCSS that this is actual SCSS code and not a random string you need to use interpolation (like if you use SCSS variables inside calc): 要指示SCSS这是实际的SCSS代码而不是随机字符串,您需要使用插值(例如,如果您在calc中使用SCSS变量):

--hover-bg-color: #{map-getter($theme-dark, AppNav, hover)};

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

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