【问题标题】:LESS transition mixinLESS 过渡混合
【发布时间】:2013-02-05 22:24:05
【问题描述】:

有人可以展示如何使用以下 LESS 混合来以 linear 轻松转换 .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;
}

【问题讨论】:

    标签: less css-transitions mixins


    【解决方案1】:

    更新:LESS 1.4+ 功能

    对于 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]

    显然,具体情况取决于您的具体实施。但是,这大致说明了您将如何使用它:

    .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;
    

    这对属性不正确。

    【讨论】:

    • 这里似乎有一个错误:所有的转换属性都应该被转换替换,因为您使用的是带有多个参数(属性持续时间和缓动类型)的快捷方式。
    • @arthur.sw:这是一个很好的收获。我只是遵循了 OP 的原始代码(其中有 transition-property)并让 LESS 传递了值,甚至没有注意到调用设置时间和缓动也是错误的“属性”本身。我现在要解决这个问题。
    猜你喜欢
    • 2015-02-21
    • 2014-01-29
    • 1970-01-01
    • 1970-01-01
    • 2013-03-23
    • 2014-07-06
    • 2013-11-05
    • 1970-01-01
    • 2014-06-07
    相关资源
    最近更新 更多