\`function v / s \`mixin在Sass-lang中。使用哪一个?

ass CSS

Gil

2020-03-24

在搜索@function和@mixin之间的差异后,我最终到了这里。

与@funcion相比,使用@mixin有什么优势吗?反之亦然。在什么情况下它们会有所不同,如何互换使用它们,请提供示例。

第3438篇《\`function v / s \`mixin在Sass-lang中。使用哪一个?》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

1个回答
宝儿理查德 2020.03.24

函数特别有用,因为它们返回值。Mixins不同于函数,它们通常仅提供有价值的代码块。

通常,在某些情况下您可能必须同时使用两者。

例如,如果我想用SASS创建一个长阴影,我将调用如下函数:

@function makelongshadow($color) {
  $val: 0px 0px $color;
  @for $i from 1 through 200 {
    $val: #{$val}, #{$i}px #{$i}px #{$color};
  }
  @return $val;
}

然后将使用此mixin进行调用:

@mixin longshadow($color) {
  text-shadow: makelongshadow($color);
}

Which provides us with the actual code.

That gets included in the element:

h1 {
    @include longshadow(darken($color, 5% ));
}

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android