简体   繁体   English

如何将变量传递给LESS参数mixin进行渐变?

[英]How to pass variables to LESS parametric mixin for gradient?

This should be a really easy thing to find but I am just not having any luck. 这应该是一个非常容易找到的东西,但我没有运气。 I want to create a parametric mixing for a linear gradient, and have some variables with default values that I can change later by passing new variables when I call it. 我想为线性渐变创建一个参数混合,并且有一些带有默认值的变量,我可以稍后通过在调用它时传递新变量来更改这些变量。 But for some reason it is giving me a syntax error when I try to pass variables. 但由于某种原因,当我尝试传递变量时它会给我一个语法错误。

Here is my mixin: 这是我的mixin:

.gradient (@startColor: #adc7e7; @endColor: #ffffff) {
background-color: @endColor;
background: -webkit-gradient(linear, left top, left bottom, from(@startColor), to(@endColor));
background: -webkit-linear-gradient(top, @startColor, @endColor);
background: -moz-linear-gradient(top, @startColor, @endColor);
background: -ms-linear-gradient(top, @startColor, @endColor);
background: -o-linear-gradient(top, @startColor, @endColor);
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='@startcolor', endColorstr='@endColor', GradientType=1);
-ms-filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='@startColor',endColorstr='@endColor',GradientType=1);
}

When I call it like this, it's fine (it just uses the default colors)... 当我这样称它时,它很好(它只使用默认颜色)...

.test {background: .gradient; }

But when I call it like this to change the from or to colors, I get a syntax error when compiling... 但是当我这样称它来改变颜色时,我会在编译时遇到语法错误...

.test {background: .gradient(#eeeeee,#ffffff); }
/* semicolon between the values don't work either */

I've tried all different ways of writing this and am not having any luck. 我已经尝试了所有不同的写作方式,但没有任何运气。 The LESS documentation does not help at all, it is using @arguments variable which I can't see how to use for a gradient. LESS文档根本没用,它使用@arguments变量,我看不到如何使用渐变。

I'm using the LESS compiler (from incident57) for mac, version 2.8, build 969. 我正在使用LESS编译器(来自incident57)for mac,版本2.8,build 969。

You should be including your mixin like this: 你应该像这样包括你的mixin:

.test {
  .gradient; 
}

The following works for me: 以下适用于我:

@blue: blue;
@red: red;

body {
   .gradient(@blue, @red);
}

Codepen Codepen

More details in the parametric mixins doc 参数mixins doc中的更多细节

You have Two Main Issues 你有两个主要问题

First , you are already including the background properties in your mixin, which is correct syntax, but makes it incorrect to call it like you are as the value of the property : 首先 ,您已经在mixin中包含了background属性,这是正确的语法,但是调用它不正确 ,就像您作为属性一样:

.test {background: .gradient(#eeeeee,#ffffff); }

Rather, it should be like this: 相反,它应该是这样的:

.test {.gradient(#eeeeee,#ffffff); }

Second , your two filter calls need to have the syntax for the variables called differently because they are nested inside of single quotes. 其次 ,您的两个filter调用需要具有不同调用的变量的语法,因为它们嵌套在单引号内。 So they should change to teh following (note the { brackets } around the name of the variables): 所以他们应该改为以下(请注意变量名称周围的{ bracket } ):

 filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='@{startColor}', endColorstr='@{endColor}', GradientType=1);
  -ms-filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='@{startColor}',endColorstr='@{endColor}',GradientType=1);

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

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