较少的过渡混合

时间:2013-02-20 19:07:49

标签: less css-transitions mixins

有人可以展示如何使用以下LESS mixin将 .25s 的以下属性转换为线性轻松吗?

border-top:6px solid#ff3300;

.transition-properties(...) {
@props: ~`"@{arguments}".replace(/[\[\]]/g, '')`;
-webkit-transition-property: @props;
-moz-transition-property: @props;
-o-transition-property: @props;
transition-property: @props;
}

1 个答案:

答案 0 :(得分:28)

更新:LESS 1.4+ Capability

使用LESS 1.4,不需要在逗号分隔参数的原始答案中使用的javascript。相反,在参数字符串末尾使用“虚拟”分号会导致逗号被视为列表分隔符,而不是参数分隔符,因此现在可以在输入多个转换时使用:

少1.4 +

mixin调用中的分号(.transition-properties(border-top .25s linear, color .5s linear;);)非常重要。如果它被省略,则两个参数之间的逗号将被删除,最终会出现无效的css规则。

.transition-properties(...) {
  -webkit-transition: @arguments;
  -moz-transition: @arguments;
  -o-transition: @arguments;
  transition: @arguments;
}


.yourClass {
  border-top: 1px solid black;
  .transition-properties(border-top .25s linear, color .5s linear;); /* semicolon is necessary */
}                                                                |
                                                          NOTE THIS SEMICOLON

.yourClass:hover {
  border-top: 6px solid #ff3300;
}

CSS输出

请注意,逗号位于两个属性值之间:

.yourClass {
  border-top: 1px solid black;
  -webkit-transition: border-top 0.25s linear, color 0.5s linear;
  -moz-transition: border-top 0.25s linear, color 0.5s linear;
  -o-transition: border-top 0.25s linear, color 0.5s linear;
  transition: border-top 0.25s linear, color 0.5s linear;
}
.yourClass:hover {
  border-top: 6px solid #ff3300;
}

原始答案[Pre LESS 1.4]

显然,细节取决于您的确切实施。但是,这一般说明了如何使用它:

LESS

.transition-properties(...) {
@props: ~`"@{arguments}".replace(/[\[\]]/g, '')`;
-webkit-transition: @props;
-moz-transition: @props;
-o-transition: @props;
transition: @props;
}


.yourClass {
  border-top: 1px solid black;
  .transition-properties(border-top .25s linear);
}

.yourClass:hover {
  border-top: 6px solid #ff3300;
}

CSS输出

.yourClass {
  border-top: 1px solid black;
  -webkit-transition: border-top 0.25s linear;
  -moz-transition: border-top 0.25s linear;
  -o-transition: border-top 0.25s linear;
  transition: border-top 0.25s linear;
}
.yourClass:hover {
  border-top: 6px solid #ff3300;
}

<强> See Example Fiddle

说明

什么

@props: ~`"@{arguments}".replace(/[\[\]]/g, '')`;

允许你进行多个逗号分隔的转换,例如:

.transition-properties(border-top .25s linear, color 1s linear);

将编译它们以逗号分隔(例如只显示一行):

transition: border-top 0.25s linear, color 1s linear;

如果您刚刚使用直线@arguments,最终会使用 无逗号分隔

transition: border-top 0.25s linear color 1s linear;

该属性不正确。