UPDATE:working CodePen利用CSS可变溶液。
更新:CSS-Tricks explains该CSS变量级联,而当他们改变了浏览器重新绘制。预处理程序变量不具备这些功能。
很难把这个明确的措词...是有可能在萨斯覆盖全局变量与所述后全局变量通过混入被列入一个局部变量?
即:我期待一个全局变量$color
设置默认值,像$red: red;
和$blue: blue;
特定颜色变量的值。然后使用全球$color
一个mixin里面,然后@include
内.class {}
是混入,然后用像$color
父组合程序内的局部变量覆盖全球.class { &.one { $color: $red; } &.two { $color: $blue; }}
的价值
全球$color
的原始值呈现,而不是改变到局部变量的值。我研究!default
和!global
但没有发现太多,在我的案件有帮助。
谢谢!
听起来对CSS变量工作:
body {
padding: 1rem;
width: 1000px;
margin: 0 auto;
}
$color: #000000;
$blue: #0087ff;
$red: #ff2828;
$yellow: #ffe607;
@mixin item {
display: inline-block;
margin: 0 1rem 1rem 0;
width: 8rem;
height: 8rem;
border-radius: 4px;
background-color: var(--c,$color);
}
@mixin circle {
border-radius: 50%;
}
.item {
@include item;
&.one {
--c: #{$blue};
}
&.two {
--c: #{$red};
@include circle;
}
&.three {
--c: #{$yellow};
@include circle;
}
}
全编译的代码:
body {
padding: 1rem;
width: 1000px;
margin: 0 auto;
}
.item {
display: inline-block;
margin: 0 1rem 1rem 0;
width: 8rem;
height: 8rem;
border-radius: 4px;
background-color: var(--c, #000000);
}
.item.one {
--c: #0087ff;
}
.item.two {
--c: #ff2828;
border-radius: 50%;
}
.item.three {
--c: #ffe607;
border-radius: 50%;
}
<body>
<div class="item one"></div> <!-- blue square -->
<div class="item two"></div> <!-- red circle -->
<div class="item three"></div> <!-- yellow circle -->
</body>
我认为这是行不通的,因为此刻你定义background-color
$color
的值黑色,所以所有的形状是黑色的。在接下来的步骤中,编译器读取$color
在不同的子类的新的价值,但这种价值从未reasigned到background-color
。如果你重新分配它像这样它的工作:
body {
padding: 1rem;
width: 1000px;
margin: 0 auto;
}
$color: #000000;
$blue: #0087ff;
$red: #ff2828;
$yellow: #ffe607;
@mixin item {
display: inline-block;
margin: 0 1rem 1rem 0;
width: 8rem;
height: 8rem;
border-radius: 4px;
background-color: $color;
}
@mixin circle {
border-radius: 50%;
}
.item {
@include item;
background-color:$color;
&.one {
$color: $blue ;
background-color: $color;
}
&.two {
$color: $red;
background-color: $color;
@include circle;
}
&.three {
$color: $yellow;
background-color: $color;
@include circle;
}
}
还是不喜欢它毯螅