css - 在 SASS 中通过引用传递函数或 mixin

标签 css sass

在 SASS 中,有什么方法可以通过引用另一个函数或 mixin 来传递函数或 mixin,然后调用引用的函数或 mixin?

例如:

@function foo($value) {
    @return $value;
}

@mixin bob($fn: null) {
    a {
        b: $fn(c); // is there a way to call a referenced function here?
    }
}

@include bob(foo); // is there any way I can pass the function "foo" here?

最佳答案

函数和混合在 Sass 中不是一流的,这意味着您不能像使用变量那样将它们作为参数传递。

Sass 3.2 及更早版本

最接近的方法是使用 @content 指令(Sass 3.2+)。

@mixin foo {
    a {
        @content;
    }
}

@include bob {
    b: foo(c); // this replaces `@content` in the foo mixin
}

唯一需要注意的是 @content 看不到你的 mixin 里面有什么。换句话说,如果 c 仅在 bob 混合宏中定义,它基本上不会存在,因为它不在范围内。

Sass 3.3 及更新版本

从 3.3 开始,您可以使用 call() 函数,但它只能与函数一起使用,不能与 mixins 一起使用。这需要传递包含函数名称的字符串作为第一个参数。

@function foo($value) {
    @return $value;
}

@mixin bob($fn: null) {
    a {
        b: call($fn, c);
    }
}

@include bob('foo');

关于css - 在 SASS 中通过引用传递函数或 mixin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14295595/

相关文章:

magento - 无法在Magento CE 1.9中编译rwd skin SCSS

javascript - 如何确保使用css和js的动态文本可以缩放和向上滑动?

css - 除了 css 中的第一段之外,我如何对每一段的开头进行文本缩进?

sass - 在 SCSS 中动态调用变量

php - TinyMCE 可以更新文本但不能更新图像,这是为什么?

html - 如何使用CSS为png图像的轮廓着色?

javascript - 如何使用 jQuery 定位单个元素

html - 结合 Foundation css 网格列定义以供重用

css - 按钮图像不起作用

css - 使用 SASS 和 twig 更改菜单的背景颜色以显示事件页面