less css calling dynamic variables from a loop

后端 未结 2 982
花落未央
花落未央 2021-01-13 15:53

What I\'m trying to do: I have (for now) 7 colors as variables. I want to be able to use them at several places and iterate throught them.

T

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-13 16:15

    I have just been trying todo the same thing today with LESS. The solution I came up with is to use a Parametric Mixin to create (define) the variable, see updated exmaple:

    @color1:#06A1C0;
    @color2:#8F4F9F;
    @color3:#ED1D24;
    @color4:#5A9283;
    @color5:#B38C51;
    @color6:#EC008C;
    @color7:#8F4F9F;
    
    @iterations: 7;
    
    .define(@var) {
      @colorSet: 'color@{var}';
    }
    
    
    .mixin-loop (@index) when (@index > 0) {
    color@{index}:hover{
        .define(@index);
        color:  @@colorSet;
      }
    
    .mixin-loop(@index - 1);
    }
    .mixin-loop (0) {}
    .mixin-loop(@iterations);
    

    Hope this helps.

提交回复
热议问题