首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >lessphp -使用函数返回变量

lessphp -使用函数返回变量
EN

Stack Overflow用户
提问于 2013-08-20 21:10:14
回答 1查看 145关注 0票数 0

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

代码语言:javascript
复制
.function(@x,@y) {

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

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

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

  property: @output;

}

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

EN

回答 1

Stack Overflow用户

发布于 2013-09-03 05:46:46

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

这样说:

变量仅在其当前作用域或任何封闭作用域中可见。

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

代码语言:javascript
复制
.function(@x,@y) {
  .innerFunction() when (@x = @y) {
    .output(@x);
  }
  .innerFunction() when not (@x = @y) {
    .output(@x/10);
  }
  .output(@output) {
    property: @output;
  };
  .innerFunction();
}

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

代码语言:javascript
复制
.test-class {
  .function(20,11);
}

将为您提供以下CSS:

代码语言:javascript
复制
.test-class {
  property: 2;
}

当然,如何构建更复杂的mixins有许多不同的方法,但解决方案/方法将取决于您在特定情况下到底想要实现什么。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18335952

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档