css - 通过 less 生成 CSS 组

标签 css less less-mixins

是否能够创建这样一个生成 CSS 组的 mixin?我将在下面解释我的意思:

.fancymixin(@max, @prefix) {
     //content what I don't know how to code
}

.fancymixin(10, x);

它生成类似的东西:

.x10, .x9, .x8, .x7, .x6, .x5, .x4, .x3, .x2, .x1 {
     //some fancy style I want to set
}

最佳答案

您可以使用一个循环(使用 protected 混合创建)和一个基类,如下所示。基类具有共同的属性,并且可以根据需要从循环内扩展任意多次。

创建 .x1, .x2, .x3{} 格式的 CSS 需要基类和扩展。如果它可以是 .x1{} .x2{},那么基类和扩展就不是真正需要的了。

.x1{ //base class with all common props
  color: blue;
} 

.fancymixin(@max, @prefix) when (@max > 1) { // loop from 10 to 1
    .fancymixin(@max - 1, @prefix); // call the next iteration of the loop
    .@{prefix}@{max}:extend(.x1){}; // extend the properties of the x1 base class
}

.fancymixin(10, x);

编译后的 CSS:

.x1,
.x2,
.x3,
.x4,
.x5,
.x6,
.x7,
.x8,
.x9,
.x10 {
  color: blue;
}

注意:如果我们想调用同一个 mixin 来创建另一个循环(比如 .fancymixin(10, y))来创建一个.y* 组的一组单独的属性,因为我们总是扩展 .x1 类属性。

关于css - 通过 less 生成 CSS 组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25403974/

相关文章:

javascript - 如何在 .less 完成解析后启动 Jquery 事件

less - 如何重用其选择器使用串联形成的混入

css - 什么是最有用的媒体 ="print"特定的、跨浏览器兼容的 css 属性?

css - 自定义形状 div,我是否有可能被正方形卡住

html - 具有固定高度和水平滚动的 ListView 元素

php - 用于将答案存储在数组中并更改按钮的颜色

LESS 文件中的 Javascript 计算错误

css - 如何在 Less 中使用字符串插入映射值

LESS mixin 调用另一个 mixin

css - 动态 LESS 混入