简体   繁体   English

循环遍历 Less 中的 mixin 参数

[英]Loop through mixin parameter in Less

I am trying to create a Less mixin for vendor properties that allows somebody to specify what CSS property they want to use, the value of the property, and what vendor(s) they want it for (Opera, Mozilla, Firefox, Webkit, IE, none).我正在尝试为供应商属性创建一个 Less mixin,它允许某人指定他们想要使用的 CSS 属性、属性的值以及他们想要的供应商(Opera、Mozilla、Firefox、Webkit、IE) , 没有任何)。

I originally wrote the code in SASS here but am having a hard time porting it to Less.我最初在这里用 SASS 编写代码,但很难将它移植到 Less。

Here is what I currently have:这是我目前拥有的:

.vendor(@property, @value, @vendors...) {
  .vendor-detect() when (@vendors = webkit) {
    -webkit-@{property}: @value; 
  }

  .vendor-detect() when (@vendors = moz) {
    -moz-@{property}: @value; 
  }

  .vendor-detect() when (@vendors = ms) {
    -ms-@{property}: @value; 
  }

  .vendor-detect() when (@vendors = o) {
    -o-@{property}: @value; 
  }

  .vendor-detect() when (@vendors = official) {
    @{property}: @value; 
  }

  .vendor-detect();
}

Right now, the when you use the code as such:现在,当您使用代码时:

.button { 
    .vendor(border-radius, 4px, official);
}

You get:你得到:

.button {
  border-radius: 4px;
}

But I want to be able to declare multiple vendors with the mixin, so using:但我希望能够使用 mixin 声明多个供应商,因此使用:

.button { 
    .vendor(border-radius, 4px, webkit moz official);
}

Should provide me with:应该给我提供:

.button {
  -webkit-border-radius: 4px;
  -moz-border-radius: 4px;
  border-radius: 4px;
}

But right now it doesn't.但现在没有。

So how do I go about looping through the vendors parameter in this mixin, or can I even do that in Less?那么,如何去通过循环vendors参数在此混入,或者我甚至可以这样做,在少?

You can loop through the vendors parameter using the below method.您可以使用以下方法遍历vendors参数。 The code is pretty straight forward to understand but I have added some inline comments to make it easier.代码很容易理解,但我添加了一些内联注释以使其更容易理解。

LESS:较少的:

.vendor(@property, @value, @vendors...) {
    .loop-vendors(@vendorCount) when (@vendorCount > 0){ // for loop for creating the req prefixes 
        .loop-vendors(@vendorCount - 1); // call the next iteration
        @vendor: extract(@vendors, @vendorCount); //extract the value from vendors list based on loop index
        -@{vendor}-@{property}: @value; // populate the vendor specific versions.
    }
    .loop-vendors(length(@vendors)); // call the loop based on length of vendors list

    @{property}: @value; //populate the official value finally
}


.button { 
    .vendor(border-radius, 4px, webkit moz ms o); // calling the vendor mixin
    .vendor(box-shadow, 1px 1px 4px gray, webkit moz ms o); // calling the vendor mixin
}

Compiled Output:编译输出:

.button {
  -webkit-border-radius: 4px;
  -moz-border-radius: 4px;
  -ms-border-radius: 4px;
  -o-border-radius: 4px;
  border-radius: 4px;
  -webkit-box-shadow: 1px 1px 4px #000000;
  -moz-box-shadow: 1px 1px 4px #000000;
  -ms-box-shadow: 1px 1px 4px #000000;
  -o-box-shadow: 1px 1px 4px #000000;
  box-shadow: 1px 1px 4px #000000;
}

Codepen Demo代码笔演示

Additional Info:附加信息:

  1. seven-phases-max has created a wrapper mixin for mimicking a for loop in LESS and a sample on that can be found in this thread . Seven-phases-max创建了一个包装器 mixin 来模拟 LESS 中的 for 循环,并且可以在此线程中找到一个示例。 It is a very simple yet effective one and I recommend you to have a look at it.这是一个非常简单但有效的方法,我建议您看一看。 I have not used that in the sample code above because I wanted to show the most basic way of doing a loop.我没有在上面的示例代码中使用它,因为我想展示执行循环的最基本方法。 In the comments, he has also kindly contributed this gist which makes use of the for wrapper.在评论中,他还善意地贡献了这个使用for包装器的要点

  2. This is another generic way to add vendor prefixes in LESS but it doesn't handle selectively based on the list of vendor prefixes required. 是在 LESS 中添加供应商前缀的另一种通用方法,但它不会根据所需的供应商前缀列表进行选择性处理。

  3. In the above sample, the official keyword is not required in the vendors list because it is auto-populated.在上面的示例中, vendors列表中不需要official关键字,因为它是自动填充的。 It is a good practice to always leave it there to be future proof.始终将其留在那里以备将来证明是一种很好的做法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM