【问题标题】:Less css: Maximum call stack size with loop in loop更少的css:循环中循环的最大调用堆栈大小
【发布时间】:2015-02-06 18:05:33
【问题描述】:

所以我想要更少地构建我的网格。 我使用 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));,我会得到另一个错误。

错误:无法读取文件/assets/less/grid.less 中未定义的属性“分母” 行号54

但是,当我手动运行 mixin 时,我的工作就像一个魅力。 例如

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

如果我在没有 .generate-columns.generate-offset 的情况下运行 .loop 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));

【问题讨论】:

  • 首先,您的循环将运行四次,但只有三个断点。要查看可能存在的其他错误,您应该向我们展示您的完整代码。 @prefixes@columns 变量有什么作用?
  • 你当然是对的(现在改回来了,那只是为了测试)。我现在添加了其他 2 个变量。这有帮助吗?

标签: css loops less


【解决方案1】:

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

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

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

@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);
}

【讨论】:

    猜你喜欢
    • 2021-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-12
    • 1970-01-01
    相关资源
    最近更新 更多