Dynamic class names in LESS

后端 未结 2 1968
独厮守ぢ
独厮守ぢ 2021-02-14 12:19

I have the following bit of LESS code working

            @iterations: 940;
            @iterations: 940;
            @col:2.0833333333333333333333333333333%;
           


        
2条回答
  •  Happy的楠姐
    2021-02-14 13:11

    I don't think you're far off. What I've done is create a second variable inside the mixin, called @index2. All this does is find the '920px minus @index' value that you're looking for:

    @index2 = (920-@index);
    

    this is then appended to the class name:

    (~".gs@{index}-@{index2}") {
    

    This is the complete loop:

    .loopingClass (@index) when (@index > 160) {
        @index2 = (920-@index);
        // create the actual css selector, example will result in
        // .myclass_30, .myclass_28, .... , .myclass_1
        (~".gs@{index}-@{index2}") {
        // your resulting css
            width: (@index/20+1)*@col;
        }
        // next iteration
        .loopingClass(@index - 60);
    }
    // "call" the loopingClass the first time with highest value
    .loopingClass (@iterations);
    

    In order to get just the set you are looking for (gs220-700 to gs700-220), just change @iterations to equal 700.

    Worth noting that currently, this will create the classes in the reverse order of how you specified them in the question.

提交回复
热议问题