【问题标题】:How to generate CSS with loop in less如何在 less 中生成带有循环的 CSS
【发布时间】:2021-03-30 23:44:55
【问题描述】:

我不熟悉Less。在我的理解中,我认为Less 可以将less 格式文件转换为标准的css 文件(如果我错了,请纠正我)。现在我有一个问题。

假设您将在单个 CSS 文件中生成 100 个如下所示的 CSS 类(从 .span1.span100)。我想知道less是否可以生成类似的CSS文件?

...
.span5 {
  width: 5%;
}

.span4 {
  width: 4%;
}

.span3 {
  width: 3%;
}

.span2 {
  width: 2%;
}

.span1 {
  width: 1%;
}

【问题讨论】:

  • 这是一个完全合理的问题,并且正是我的问题。请重新打开。

标签: css less


【解决方案1】:

试试这个:

@iterations: 5;
.span-loop (@i) when (@i > 0) {
    .span-@{i} {
        width: ~"@{i}%";
    }
    .span-loop(@i - 1);
}
.span-loop (@iterations);

输出:

.span-5 {
  width: 5%;
}
.span-4 {
  width: 4%;
}
.span-3 {
  width: 3%;
}
.span-2 {
  width: 2%;
}
.span-1 {
  width: 1%;
}

您可以在less2css 上试用。

2014 年 4 月 3 日编辑

这是一个更灵活的版本,有更多选项:

.custom-loop( @base-value:@n ; @n ; @unit : px; @j : 1 ;@_s:0 ; @step-size:1  ; @selector:~".span-"; @property : width)
when not(@n=0)  {

  @size : @base-value+@_s;
  @{selector}@{j}{
    @{property} : ~"@{size}@{unit}";
  }
  .custom-loop(@base-value ,  (@n - 1), @unit , (@j + 1) ,  (@_s+@step-size) , @step-size,  @selector, @property);
}

您只能通过@n 调用它,这是必需的参数:

.custom-loop(@n:3);

将输出:

.span-1 {
  width: 3px;
}
.span-2 {
  width: 4px;
}
.span-3 {
  width: 5px;
}

但是如果你想控制每个参数,这里是一个使用所有自定义参数的例子:

.custom-loop( @n: 3 , @base-value:1, @unit: '%', @property:font-size, @selector: ~".fs-", @step-size: 2);

这将输出:

.fs-1 {
  font-size: 1%;
}
.fs-2 {
  font-size: 3%;
}
.fs-3 {
  font-size: 5%;
}

参数说明

  1. @n整数,迭代次数。

  2. @base-value(可选):整数,要分配给属性的循环的起始值。默认值与分配给迭代次数 @n 的值相同。

  3. @unit(可选):字符串,属性的单位。默认值为px

  4. @property(可选):non-stringstring CSS 属性。默认值为width

  5. @selector(可选):转义字符串,用于循环的选择器。可以是任何东西,只要它作为转义字符串传入即可。

  6. @step-size(可选):整数,循环递增的值。

注意事项

注意 1: 自定义选择器作为转义字符串传入。如果不转义,就不会按预期工作。

注意2: mixin 是通过显式使用参数名称来调用的。这有一些优点和缺点:

注3:单位作为字符串传入。

优势

  1. 叫什么参数一目了然
  2. 您不必依赖参数的顺序并记住哪个参数先出现,第二个出现......

缺点

  1. 我猜它看起来有点丑?
  2. (如果您知道更好的实现,请添加到列表和/或更改实现)

【讨论】:

  • 只有这种语法适用于less.php (leafo),谢谢!
  • 一个小小的批评。我更喜欢@j:@base-value(@j+@step-size),这样我可以获得有意义的类名,而不是没有上下文的索引。在您的解决方案中,当 base-value != step-size != 1 时,类名与属性中设置的值不匹配。
【解决方案2】:

请注意,自 3.7 版以来,Less 有一个 each() 函数,可以很容易地与 range() 函数一起使用来生成所需的代码 - 如下所示:

each(range(100),{
    .span@{value} { width: @value * 1%; }
});

【讨论】:

    【解决方案3】:

    在给定的方法中是不可能做到的。

    但可能是这样的:

    .loopingClass (@index) when (@index > 0){
      .span_@{index}{
        width: @index px;
      }
      .loopingClass(@index - 1);
    }
    .loopingClass(5);
    

    Resilt 会是这样的:

    .span_100 {
      width: 100 px;
    }
    .span_99 {
      width: 99 px;
    }
    .span_98 {
      width: 98 px;
    }
    .span_97 {
      width: 97 px;
    }
    .span_96 {
      width: 96 px;
    }
    .span_95 {
      width: 95 px;
    }
    
    and e.t.c.
    

    【讨论】:

    • 请用代码或小提琴添加答案。代码上的图片被认为是错误的答案
    • 这个答案不是@Amin 答案的副本吗?
    【解决方案4】:

    所有,我找到了一种循环输出 css 的方法。请检查一下。谢谢。

    @iterations: 100;
    
    // helper class, will never show up in resulting css
    // will be called as long the index is above 0
    .loopingClass (@index) when (@index > 0) {
    
        // create the actual css selector, example will result in
        // .myclass_30, .myclass_28, .... , .myclass_1
        (~".span@{index}") {
            // your resulting css
            width: percentage((@index - 1) *0.01);
        }
    
        // next iteration
        .loopingClass(@index - 1);
    }
    
    // end the loop when index is 0
    .loopingClass (0) {}
    
    // "call" the loopingClass the first time with highest value
    .loopingClass (@iterations);
    

    【讨论】:

    • 请参考Bootstrap的Less源码(即mixins.less)。搜索 ".spanX" 字符串,我希望它在您要获取的内容旁边。
    • 您使用 guard 和递归 mixin,就像 Bootstrap 的作者一样。
    • 请前往此帖:Do a loop with LESS css
    • @Joe.wang 代码在 less2css.org 中不起作用。所以,我玩了,得到了这个:` @iterations: 100; .loopingClass (@index) when (@index > 0) { .span@{index} { width: percent((@index ) *0.01); } .loopingClass(@index - 5); } .loopingClass (0) {} .loopingClass (@iterations);` @iterations: 100; .loopingClass (@index) when (@index > 0) { .span@{index} { width: percent((@index ) *0.01); } .loopingClass(@index - 5); } .loopingClass (0) {} .loopingClass (@iterations); `
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-21
    • 2017-07-02
    • 1970-01-01
    • 2014-08-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多