Less css:循环中的最大调用堆栈大小

时间:2015-01-31 22:54:40

标签: css loops less

所以我想减少构建我的网格。 我使用less页面(http://lesscss.org/features/#loops-feature)中描述的columns方法。但是当我运行它时出现错误。

  

错误:文件/assets/less/grid.less中超出了最大调用堆栈大小   行号54

第54行是我发起循环.loop(@grids, (@grids + 1));

的地方

如果我删除了mixin中的.generate-offset(@n, @tag, (@i + 1));,我会收到另一个错误。

  

错误:无法读取属性&分母'未定义的文件/资产/更少/ grid.less   行号54

然而,当我手动运行mixin时,我的工作就像一个魅力。 e.g。

.generate-columns(2, xs);
.generate-offset(2, xs);

如果我在没有.loop.generate-columns mixins的情况下运行.generate-offset mixin,它也可以正常运行3次(由于3个断点)。

任何想法,为什么我在组合两者时都会遇到这些错误?

@prefixes: 'sm', 'md', 'lg';
@breakpoints: '0', '100rem', '140rem';
@columns: '2','6','12';

.generate-offset(@n, @tag, @i: 1) when (@i < @n) {
  .offset--@{tag}-@{i} {
    margin-left: (@i * 100% / @n);
  }
  .generate-offset(@n, @tag, (@i + 1));
}

// Grid loops

.loop(@index, @count) when (@index > 0){
    // extract variables
    @current: (@count - @index);
    @prefix: e(extract(@prefixes, @current));
    @breakpoint: e(extract(@breakpoints, @current));
    @column: e(extract(@columns, @current));

    @media (min-width: @breakpoint) {
      .generate-columns(@column, @prefix);
      .generate-offset(@column, @prefix);
    }

    .loop ((@index - 1), @count);
}

// run
@grids: length(@breakpoints);
.loop(@grids, (@grids + 1));

解决方案:

万一有人遇到同样的问题,我的最终代码现在看起来像这样。

@prefixes: sm, md, lg;
@breakpoints: 0, 100rem, 140rem;
@columns: 2,6,12;
// ********************
// Column Mixin
//
.generate-columns(@n, @tag, @i: 1) when (@i =< @n) {
  .column--@{tag}-@{i} {
    flex: 0 0 (@i * 100% / @n);
  }
  .generate-columns(@n, @tag, (@i + 1));
}
// Offset Mixin
//
.generate-offset(@col, @tag, @i: 1) when (@i < @col) {
  .offset--@{tag}-@{i} {
    margin-left: (@i * 100% / @col);
  }
  .generate-offset(@col, @tag, (@i + 1));
}
// Make grid
//
.make-grid(@breakpoint, @cols, @pref) {
  & when( @breakpoint > 0 ){
    @media(min-width: @breakpoint) {
      .generate-columns(@cols, @pref);
      .generate-offset(@cols, @pref);
    }
  }
  & when( @breakpoint = 0 ){
    .generate-columns(@cols, @pref);
    .generate-offset(@cols, @pref);
  }
}
// Run make-grid for every breakpoint
//
.loop(@index) when (@index > 0){
    // run loop first to change order
    .loop ((@index - 1));

    .make-grid(
      extract(@breakpoints, @index),
      extract(@columns, @index),
      extract(@prefixes, @index)
    );
}
.loop(length(@breakpoints));

1 个答案:

答案 0 :(得分:2)

您的案例中的问题是因为e()(或~())函数的输出始终是字符串,您无法使用它来执行数学运算或比较等。您可以通过在@media查询中添加以下行(并注释掉mixin调用)来验证这一点。

columnIsNumber: isnumber(@column); 
/* with your current method this will always return false */

要解决此问题,您应该避免对要执行数学运算的任何变量使用e()函数。例如,您可以将mixin更改为如下所示(请参阅对所做更改的内联注释):

@prefixes: 'sm', 'md', 'lg';
@breakpoints: '0', '100rem', '140rem';
@columns: 2, 6, 12; /* note the exclusion of quotes */

.generate-offset(@n, @tag, @i: 1) when (@i < @n) {
  .offset--@{tag}-@{i} {
    margin-left: (@i * 100% / @n);
  }
  .generate-offset(@n, @tag, (@i + 1));
}

// Grid loops

.loop(@index, @count) when (@index > 0){
    // extract variables
    @current: (@count - @index);
    @breakpoint: e(extract(@breakpoints, @current));
    @column: extract(@columns, @current); /* avoid using e() */
    @prefix: e(extract(@prefixes, @current));
    @media (min-width: @breakpoint) {
        .generate-columns(@column, @prefix);
        /*columnIsNumber: isnumber(@column);*/
        .generate-offset(@column, @prefix);
    }

    .loop ((@index - 1), @count);
}

// run
@grids: length(@breakpoints);
.loop(@grids, (@grids + 1));

当然,您可以使用自己的代码(使用变量声明中的引号和mixin中的e())使用一些JS评估来完成。但我不推荐这种方法,因为它只会增加我视图中的复杂性。

@media (min-width: @breakpoint) {
    .generate-columns(@column, @prefix);
    @col: `function(){return @{column}}()`; /* JS evaluation */
    /*columnIsNumber: isnumber(@col);*/
    .generate-offset(@col, @prefix);
}

@media (min-width: @breakpoint) {
    .generate-columns(@column, @prefix);
    @col: `function(){return parseInt(@{column},10)}()`; /* JS evaluation */
    /*columnIsNumber: isnumber(@col);*/
    .generate-offset(@col, @prefix);
}