【问题标题】:LESS CSS - Mixin Arguments and Nth-Child - without loopingLESS CSS - Mixin Arguments 和 Nth-Child - 没有循环
【发布时间】:2015-07-03 12:44:59
【问题描述】:

我正在尝试编写一个与 id #p0x1 相关联的较少参数。我希望能够写出像

这样的论点
@ytrans = translateY(@argument * 2) 

然后为每个 ID 提供该参数值。

#p0x1(@argument:3){@ytrans} 

但是,我无法使用 nth-child css 执行此操作。我只看到了用循环解决这个问题的解决方案。有没有更直接的方式来解决这个问题?

#p0x1 div:nth-child(1)(@argument:3){ 

这是我目前拥有的代码。

/*Cube Position 0,1*/

#p0x1 div:nth-child(1) {
  transform         : @zaxis @ytrans;
    background-image: linear-gradient(@gradient);
}
#p0x1 div:nth-child(2) {
  transform         : @top @zaxis @ytrans;  
    background-image: linear-gradient(@gradient);
}
#p0x1 div:nth-child(3) {
  transform         : @back @zaxis @ytrans;
    background-image: linear-gradient(@gradient);
}
#p0x1 div:nth-child(4) {
  transform         : @bottom @zaxis @ytrans;
    background-image: linear-gradient(@gradient);
}
#p0x1 div:nth-child(5) {
    transform: @left @zaxis @ytrans;
    background-image: linear-gradient(@gradient);
}
#p0x1 div:nth-child(6)  {
    transform         : @right @zaxis @ytrans;
    background-image: linear-gradient(@gradient);
}

【问题讨论】:

    标签: css css-selectors arguments less less-mixins


    【解决方案1】:

    在一个出色的个人名字seven-phases-max 的帮助下,我终于能够找到这个解决方案,他帮助我使用了我的prior problem。


    我的解决方案

    .coordinates(-2, 2);
         .coordinates(@min, @max) {
        .xcoor; .xcoor(@xcoor: @min) when (@xcoor <= @max) {
            .ycoor; .xcoor(@xcoor + 1);
        }
        .ycoor(@ycoor: @min) when (@ycoor <= @max) {
            @ytrans:translatey(@cubesize * @ycoor);
            @xtrans:translatex(@cubesize * @xcoor);
    #p@{xcoor}x@{ycoor} div:nth-child(1) {
      transform: @zaxis @ytrans @xtrans;
        background-image: linear-gradient(@gradient);
    }
    #p@{xcoor}x@{ycoor} div:nth-child(2) {
      transform: @top @zaxis @ytrans @xtrans;   
        background-image: linear-gradient(@gradient);
    }
    #p@{xcoor}x@{ycoor} div:nth-child(3) {
      transform: @back @zaxis @ytrans @xtrans;
        background-image: linear-gradient(@gradient);
    }
    #p@{xcoor}x@{ycoor} div:nth-child(4) {
      transform: @bottom @zaxis @ytrans @xtrans;
        background-image: linear-gradient(@gradient);
    }
    #p@{xcoor}x@{ycoor} div:nth-child(5) {
        transform: @left @zaxis @ytrans @xtrans;
        background-image: linear-gradient(@gradient);
    }
    #p@{xcoor}x@{ycoor} div:nth-child(6)  {
        transform: @right @zaxis @ytrans @xtrans;
        background-image: linear-gradient(@gradient);
    }
            .ycoor(@ycoor + 1);
        }
    }
    

    Output of more than 60,000 lines

    这就是我喜欢 CSS 预处理器的原因。如果有人有改进建议,我会欢迎他们。

    【讨论】:

    • 对不起,如果我误解了你的问题,但答案仍然使用循环(两个事实上)。
    • 你说得对。我的无知使我相信在每个 nth-child 节点上进行自定义时循环效率会降低。这种无知是我的错误。也就是说,有人提出了一个很好的观点,即这纯粹是实验性的。任何人都不应该为一个 Web 应用程序使用 60,000 行代码。
    猜你喜欢
    • 1970-01-01
    • 2017-05-05
    • 2012-03-14
    • 2016-12-17
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多