【问题标题】:Passing multiple arguments in SASS Mixin to output set of classes or single class将 SASS Mixin 中的多个参数传递给输出类集或单个类
【发布时间】:2018-10-06 14:48:54
【问题描述】:
$base-space: 1rem !default;

$space-map : (
    '1o9': $base-space,
    '1o8': $base-space/8,
    '1o4': $base-space/4,
    '1o2': $base-space/2,
) !default;

@mixin containers($new-space-map) {

    @each $name, $value in $new-space-map {

        .container--#{$name}{
            margin: $value 0;
        }
    }
}

@include containers($space-map);

这会输出 4 个 CSS 类。

我正在尝试根据我作为参数传递的名称生成所有 4 个类或仅一个类。

我的输出需要是:

.container--1o9 {
  margin: 1rem 0;
}

.container--1o8 {
  margin: 0.125rem 0;
}

.container--1o4 {
  margin: 0.25rem 0;
}

.container--1o2 {
  margin: 0.5rem 0;
}

或者

.container--1o2 {
  margin: 0.5rem 0;
}

基于我通过的论点。

进一步解释;我正在尝试根据我的需要以下面提到的两种方式在同一个 SASS 地图上使用@include

@include containers(<SASS map file>); //This I achieved

@include containers(<any key from SASS Map>); //This I cant

我被困在这里,不确定这是否可以实现。任何帮助将不胜感激。

【问题讨论】:

  • 尝试删除 !default。我是在 Sassmeister 做的,效果如你所愿
  • @Brad,如果我想输出一个类该怎么办。我试过@include containers($1o9),但它不起作用。我正在尝试在同一个 SASS 地图上使用 @include,具体取决于我的需要:@include containers(&lt;SASS map file&gt;);@include containers(&lt;any key from SASS Map&gt;); 如果您方便的话,请给我 Sassmeister 链接。
  • 你的 mixin 正在输出 4 个类。只需使用您要使用的类。所有这 4 个类都在您的 CSS 编译文件中
  • @Brad,明白了。我有不同的思路,这是当前代码无法实现的。感谢您清除我的想法。

标签: sass mixins extend sass-maps scss-mixins


【解决方案1】:

删除 !Default。这将按您的预期工作

链接到萨斯迈斯特

https://www.sassmeister.com/gist/2839eaf64bd441e73a92aed6bb980465

请记住,所有 4 个类都被编译到您的 CSS 文件中。只用你想要的那个

【讨论】:

  • 请发布完整的答案,而不仅仅是一个链接(链接是 404)
猜你喜欢
  • 1970-01-01
  • 2023-03-20
  • 2014-02-20
  • 1970-01-01
  • 2011-11-22
  • 1970-01-01
  • 2015-05-07
  • 1970-01-01
  • 2016-12-15
相关资源
最近更新 更多