How to pass a property name as an argument to a mixin in less

前端 未结 4 1298
说谎
说谎 2020-12-06 21:11

I want to make a function/mixin that will make a color darker if it is already dark but lighter when it is light (normalize/extremeize?)

Is it possible to do this by

4条回答
  •  旧时难觅i
    2020-12-06 21:36

    Guarded Mixins should be what you are looking for, however you can not use variables to define properties, only their values. So you can do it like this:

    .normalize(@color, @amount) when (lightness(@color) >= 50%)
    {
        color:lighten(@color, @amount);
    }
    
    .normalize(@color, @amount) when (lightness(@color) < 50%)
    {
       color:darken(@color, @amount);
    }
    

    So this:

    .class1 {
        .normalize(#ffffd, 10%);
    }
    

    Will output this:

    .class1 {
      color: #f7f7f7;
    }
    

    But you can not actually pass a property name as a variable. This is a limitation of LESS unfortunately, and while I've seen ways around it for things like margin direction, there is not a way to just pass any ol' property using a variable.

提交回复
热议问题