LESS CSS Pass mixin as a parameter to another mixin

后端 未结 4 1665
借酒劲吻你
借酒劲吻你 2020-12-08 11:49

Is there any way to pass one mixin or style\'s declaration to another mixin as an input parameter?

Let\'s take a look at an example with animation keyframes. Followi

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-08 12:40

    Simplification

    I just simplified a little ScottS' way, separateing @keframes from -animation:

    .keyframes(@name, @from, @to) {
        @newline: `"\n"`;
        .Local(@x){};
        .Local(@x) when (@x="") {(~"}@{newline}/*"){a:a}/**/};
    
        .setVendor(@pre, @vendor) {
            (~"@{pre}@@{vendor}keyframes @{name} {@{newline}from") {
                .from(@from);
            }
            to {
                .to(@to);
            }
            .Local(@vendor);
        }
        .setVendor(""            , "-webkit-");
        .setVendor(~"}@{newline}",    "-moz-");
        .setVendor(~"}@{newline}",      "-o-");
        .setVendor(~"}@{newline}",         "");
    }
    
    .animation(...) {
      -webkit-animation: @arguments;
         -moz-animation: @arguments;
           -o-animation: @arguments;
              animation: @arguments;
    }
    

    use:

    .from(a1-from){ width: 10px; }
    .to(a1-to) { width: 20px; }
    .keyframes(a1-animation, a1-from, a1-to);
    
    
    .selector {
        // some other css
        .animation(a1-animation 1s infinite linear);
    }
    

    output:

    @-webkit-keyframes a1-animation {
    from {
      width: 10px;
    }
    to {
      width: 20px;
    }
    }
    @-moz-keyframes a1-animation {
    from {
      width: 10px;
    }
    to {
      width: 20px;
    }
    }
    @-o-keyframes a1-animation {
    from {
      width: 10px;
    }
    to {
      width: 20px;
    }
    }
    @keyframes a1-animation {
    from {
      width: 10px;
    }
    to {
      width: 20px;
    }
    }
    /* {
      a: a;
    }
    /**/
    
    
    .selector {
      // some other css
      -webkit-animation: a1-animation 1s infinite linear;
      -moz-animation: a1-animation 1s infinite linear;
      -o-animation: a1-animation 1s infinite linear;
      animation: a1-animation 1s infinite linear;
    }
    

    little problem:

    So animation is now separated from @keyframes, but we got to pay the price. There is a nasty:

    /* {
      a: a;
    }
    /**/
    

    but it shouldn't be a problem -> propably all of us push CSS files through any kinds of minifiers which cut comments out.

提交回复
热议问题