0

当我使用默认的 less.js 时,我可以在函数中返回一个变量,例如:

.function(@x,@y) {

  .innerFunction() when (@x = @y) {
    @output: @x
  }

  .innerFunction() when not (@x = @y) {
    @output: @x/10;
  }

  .innerFunction() when (...) {
    @output: @x + @y;
  }

  property: @output;

}

这对于创建更复杂的 less 函数非常实用,但它不适用于 lessphp……有没有办法在 lessphp 中返回变量?

4

1 回答 1

0

Lessphp 不允许变量泄漏到定义它们的规则范围之外。

lessphp 文档是这样说的:

变量只能在其当前范围或任何封闭范围内使用。

您可以像这样重新格式化 mixin 以在 less.js 和 lessphp 中工作的方式可能是这样的:

.function(@x,@y) {
  .innerFunction() when (@x = @y) {
    .output(@x);
  }
  .innerFunction() when not (@x = @y) {
    .output(@x/10);
  }
  .output(@output) {
    property: @output;
  };
  .innerFunction();
}

例如在LESS中像这样调用mixin:

.test-class {
  .function(20,11);
}

将为您提供以下 CSS:

.test-class {
  property: 2;
}

当然,有许多不同的方法可以构建更复杂的 mixin,但解决方案/方法将取决于您在特定情况下想要实现的具体目标。

于 2013-09-02T21:46:45.750 回答