【发布时间】:2019-09-19 00:29:20
【问题描述】:
是否可以让 SASS 使用其元素样式的背景颜色作为变量以在其函数中使用?
目前我正在我们的 API 中生成这些值并将它们传递下来,但如果这是我可以用 SASS 做的事情会更简单。
@function set-text-color($color) {
@if (lightness( $color) > 40) {
@return black;
}
@else {
@return white;
}
}
.example {
.heading {
font-size: 1.5em;
padding: 1em;
// color:set-text-color('element background colour');
}
.description {
padding: 1em;
// background: lighten('element background colour', 30%);
// color:set-text-color('element background colour');
}
}
<div class="example">
<div class="heading" style="background-color: red;">
Heading
</div>
<div class="description" style="background-color: red;">
Nullam id sollicitudin mauris. Morbi vestibulum ullamcorper leo vel tristique.
</div>
</div>
<div class="example">
<div class="heading" style="background-color: yellow;">
Heading
</div>
<div class="description" style="background-color: yellow;">
Nullam id sollicitudin mauris. Morbi vestibulum ullamcorper leo vel tristique.
</div>
</div>
<div class="example">
<div class="heading" style="background-color: green;">
Heading
</div>
<div class="description" style="background-color: green;">
Nullam id sollicitudin mauris. Morbi vestibulum ullamcorper leo vel tristique.
</div>
</div>
<div class="example">
<div class="heading" style="background-color: blue;">
Heading
</div>
<div class="description" style="background-color: blue;">
Nullam id sollicitudin mauris. Morbi vestibulum ullamcorper leo vel tristique.
</div>
</div>
【问题讨论】: