我有一个类别列表,我想从中设置背景颜色.我想将背景颜色的值保留为变量.是否可以通过字符串插值引用变量?Sass正在使用此代码抛出"无效CSS"错误:
/* Category Colors */
$family_wellness_color: #c1d72e;
$lifestyle_color: #f4eb97;
$food_color: #f78f1e;
...
/* Categories */
@each $cat in family_wellness, lifestyle, food
{
.#{$cat}
{
.swatch, .bar
{
background-color: $#{$cat}_color;
}
}
}
Run Code Online (Sandbox Code Playgroud)
可能?我真的很感激一些建议!
好吧,我能得到的最接近我想要的东西是:
#_variables.scss
/* Categories */
$family_wellness_color: #c1d72e;
$lifestyle_color: #f4eb97;
$food_color: #f78f1e;
$media_entertainment_color: #db3535;
$travel_recreation_color: #e30e61;
$education_color: #92278f;
$sports_color: #0070bb;
$technology_color: #00b5cc;
$products_shopping_color: #028e99;
$companies_businesses_color: #56BA42;
#_mixins.scss
@import 'variables';
@mixin get_category_bkgd_color($category_name)
{
@if $category_name == family_wellness
{
@include bkgd_color($family_wellness_color);
}
@else if $category_name == lifestyle
{
@include bkgd_color($lifestyle_color);
}
@else if $category_name == food
{
@include bkgd_color($food_color);
}
@else if $category_name == media_entertainment
{
@include bkgd_color($media_entertainment_color);
}
@else if $category_name == travel_recreation
{
@include bkgd_color($travel_recreation_color);
}
@else if $category_name == education
{
@include bkgd_color($education_color);
}
@else if $category_name == sports
{
@include bkgd_color($sports_color);
}
@else if $category_name == technology
{
@include bkgd_color($technology_color);
}
@else if $category_name == products_shopping
{
@include bkgd_color($products_shopping_color);
}
@else if $category_name == companies_businesses
{
@include bkgd_color($companies_businesses_color);
}
}
#dashboard.scss
@import 'variables', 'mixins';
@each $cat in family_wellness, lifestyle, food, media_entertainment, travel_recreation, education, sports, technology, products_shopping, companies_businesses
{
.#{$cat}
{
.swatch, .bar
{
@include get_category_bkgd_color($cat);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这不是最优雅的解决方案,但它确实为我提供了一个可以在其他几个领域重复使用的 mixin。有谁找到一种方法可以提高效率?
| 归档时间: |
|
| 查看次数: |
4540 次 |
| 最近记录: |