sass @mixin 可以接受未定义数量的参数吗?

发布于 2024-12-12 02:13:18 字数 543 浏览 0 评论 0原文

我正在尝试创建一个用于过渡的 sass mixin。这是我到目前为止所拥有的。

@mixin transition($var)
  -webkit-transition: $var
  transition: $var

我希望能够像这样传递多个参数

@include transition(color .5s linear, width .5s linear)

不幸的是,我收到以下错误

Syntax error: Mixin transition takes 1 argument but 2 were passed.

有没有办法做到这一点,以便它在 css 中生成以下输出,同时仍然接受未定义数量的参数?

-webkit-transition: color .5s linear, width .5s linear;
transition: color .5s linear, width .5s linear;

I'm trying to create a sass mixin for transitions. This is what I have so far.

@mixin transition($var)
  -webkit-transition: $var
  transition: $var

I want to be able to pass it multiple arguments like this

@include transition(color .5s linear, width .5s linear)

Unfortunately, I get the following error

Syntax error: Mixin transition takes 1 argument but 2 were passed.

Is there a way to do this so it produces the following output in css while still accepting an undefined number of arguments?

-webkit-transition: color .5s linear, width .5s linear;
transition: color .5s linear, width .5s linear;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

他是夢罘是命 2024-12-19 02:13:18

变量参数

有时,mixin 接受未知数量的参数是有意义的。例如,用于创建盒子阴影的 mixin 可能采用任意数量的阴影作为参数。对于这些情况,Sass 支持“变量参数”,它们是 mixin 声明末尾的参数,它获取所有剩余参数并将它们打包为列表。这些参数看起来就像普通参数,但后面跟着 ...。例如:

@mixin box-shadow($shadows...) {
  -moz-box-shadow: $shadows;
  -webkit-box-shadow: $shadows;
  box-shadow: $shadows;
}

.shadows {
  @include box-shadow(0px 4px 5px #666, 2px 6px 10px #999);
}

编译为:

.shadows {
  -moz-box-shadow: 0px 4px 5px #666, 2px 6px 10px #999;
  -webkit-box-shadow: 0px 4px 5px #666, 2px 6px 10px #999;
  box-shadow: 0px 4px 5px #666, 2px 6px 10px #999;
}

From : SASS 官方文档

所以你基本上只需要更改你的mixins 声明如下所示:

@mixin transition($var...)
  -webkit-transition: $var
  transition: $var

Variable Arguments

Sometimes it makes sense for a mixin to take an unknown number of arguments. For example, a mixin for creating box shadows might take any number of shadows as arguments. For these situations, Sass supports “variable arguments,” which are arguments at the end of a mixin declaration that take all leftover arguments and package them up as a list. These arguments look just like normal arguments, but are followed by .... For example:

@mixin box-shadow($shadows...) {
  -moz-box-shadow: $shadows;
  -webkit-box-shadow: $shadows;
  box-shadow: $shadows;
}

.shadows {
  @include box-shadow(0px 4px 5px #666, 2px 6px 10px #999);
}

is compiled to:

.shadows {
  -moz-box-shadow: 0px 4px 5px #666, 2px 6px 10px #999;
  -webkit-box-shadow: 0px 4px 5px #666, 2px 6px 10px #999;
  box-shadow: 0px 4px 5px #666, 2px 6px 10px #999;
}

From : SASS official Documentation

So you basically just need to change your mixins declaration to look like this :

@mixin transition($var...)
  -webkit-transition: $var
  transition: $var
半寸时光 2024-12-19 02:13:18

当您调用 mixin 时,请像这样调用它:

@include transition( (color .5s linear, width .5s linear) );

带有额外的括号。这将使 sass 明白您希望将其用作单个参数。

编辑:如果使用 Sass 3.2 或更高版本,请参阅上面 Jeremie Parker 的回答。 3.2 中添加了实变量参数: http: //chriseppstein.github.io/blog/2012/08/23/sass-3-2-is-released

When you call the mixin, call it like this:

@include transition( (color .5s linear, width .5s linear) );

With the extra parens. This will key sass into the fact that you want this used as a single argument.

EDIT: See Jeremie Parker's answer above if using Sass 3.2 or later. Real Variable Arguments were added in 3.2: http://chriseppstein.github.io/blog/2012/08/23/sass-3-2-is-released

梦里寻她 2024-12-19 02:13:18

如果你想要多个参数供应商前缀,就像下面的场景:

@include transition(transform .5s linear, width .5s linear)

预期

-webkit-transition: -webkit-transform 0.5s linear, width 0.5s linear;
-moz-transition: -moz-transform 0.5s linear, width 0.5s linear;
-ms-transition: -ms-transform 0.5s linear, width 0.5s linear;
-o-transition: -o-transform 0.5s linear, width 0.5s linear;
transition: transform 0.5s linear, width 0.5s linear;

我建议你这个Mixin ,我发现 无意义写作

代码

@function prefix($property, $prefixes: (webkit moz o ms)) {
    $vendor-prefixed-properties: transform background-clip background-size;
    $result: ();
    @each $prefix in $prefixes {
       @if index($vendor-prefixed-properties, $property) {
         $property: -#{$prefix}-#{$property}
       }
       $result: append($result, $property);
    }
    @return $result;
}

@function trans-prefix($transition, $prefix: moz) {
    $prefixed: ();
    @each $trans in $transition {
        $prop-name: nth($trans, 1);
        $vendor-prop-name: prefix($prop-name, $prefix);
        $prop-vals: nth($trans, 2);
        $prefixed: append($prefixed, ($vendor-prop-name $prop-vals), comma);
    }

    @return $prefixed;
}

@mixin transition($values...) { 
    $transitions: ();
    @each $declaration in $values {
        $prop: nth($declaration, 1);
        $prop-opts: ();
        $length: length($declaration);
        @for $i from 2 through $length {
            $prop-opts: append($prop-opts, nth($declaration, $i));   
        }
        $trans: ($prop, $prop-opts);
        $transitions: append($transitions, $trans, comma);
    }

    -webkit-transition: trans-prefix($transitions, webkit);
    -moz-transition: trans-prefix($transitions, moz);
    -o-transition: trans-prefix($transitions, o);
    transition: $values;
}

In case you want multiple arguments and vendor-prefixed, like the fallowing scenario:

@include transition(transform .5s linear, width .5s linear)

Expected

-webkit-transition: -webkit-transform 0.5s linear, width 0.5s linear;
-moz-transition: -moz-transform 0.5s linear, width 0.5s linear;
-ms-transition: -ms-transform 0.5s linear, width 0.5s linear;
-o-transition: -o-transform 0.5s linear, width 0.5s linear;
transition: transform 0.5s linear, width 0.5s linear;

I suggest you this Mixin, I found on Meaningless Writing.

Code

@function prefix($property, $prefixes: (webkit moz o ms)) {
    $vendor-prefixed-properties: transform background-clip background-size;
    $result: ();
    @each $prefix in $prefixes {
       @if index($vendor-prefixed-properties, $property) {
         $property: -#{$prefix}-#{$property}
       }
       $result: append($result, $property);
    }
    @return $result;
}

@function trans-prefix($transition, $prefix: moz) {
    $prefixed: ();
    @each $trans in $transition {
        $prop-name: nth($trans, 1);
        $vendor-prop-name: prefix($prop-name, $prefix);
        $prop-vals: nth($trans, 2);
        $prefixed: append($prefixed, ($vendor-prop-name $prop-vals), comma);
    }

    @return $prefixed;
}

@mixin transition($values...) { 
    $transitions: ();
    @each $declaration in $values {
        $prop: nth($declaration, 1);
        $prop-opts: ();
        $length: length($declaration);
        @for $i from 2 through $length {
            $prop-opts: append($prop-opts, nth($declaration, $i));   
        }
        $trans: ($prop, $prop-opts);
        $transitions: append($transitions, $trans, comma);
    }

    -webkit-transition: trans-prefix($transitions, webkit);
    -moz-transition: trans-prefix($transitions, moz);
    -o-transition: trans-prefix($transitions, o);
    transition: $values;
}
戴着白色围巾的女孩 2024-12-19 02:13:18

Compass 有一个过渡 mixin,你可以看一下(或者你可以只使用 Compass)。您可以在这里更好地查看它: http://beta.compass- style.org/reference/compass/css3/transition/

从表面上看,你不能执行未定义数量的 mixin,因为 Compass 的维护者也帮助维护 Sass,你可以看到他为他的转换 mixin 定义了最多 10 个单独的参数。

Compass has a transition mixin that you could take a look at (or you could just use Compass). You can take a better look at it here: http://beta.compass-style.org/reference/compass/css3/transition/.

By the looks of it you can't do an undefined number of mixins as the maintainer of Compass also aids in maintaining Sass and you can see that he has defined a maximum number of 10 separate arguments for his transition mixin.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文