crossbrowser opacity mixin for .less

旧时模样 提交于 2019-12-04 03:01:51

Assuming you are using LESS 1.3.1 or later, then this does what you want (using built in functions):

LESS

//define mixin
.crossbrowser(@color,@alpha){
  @rgba: rgba(red(@color),green(@color),blue(@color),@alpha);
  @argb: argb(@rgba);
  background-color: @color;
  background-color: @rgba;
  filter: ~"progid:DXImageTransform.Microsoft.gradient(startColorstr=@{argb}, endColorstr=@{argb})";
  -ms-filter: ~"progid:DXImageTransform.Microsoft.gradient(startColorstr=@{argb}, endColorstr=@{argb})";
}

//use it
.crossbrowser(red, .2);

CSS Output

background-color: #ff0000;
background-color: rgba(255, 0, 0, 0.2);
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr=#33ff0000, endColorstr=#33ff0000);
-ms-filter: progid:DXImageTransform.Microsoft.gradient(startColorstr=#33ff0000, endColorstr=#33ff0000);

From the docs:

However, if you still want to use JavaScript in .less, this is done by wrapping the expression with back-ticks:

@var: `"hello".toUpperCase() + '!'`;

So your line should be this (I think):

@ievalue:`Math.floor(@alpha * 255).toString(16)`;

And since you're using a variable inside, you might need to use string interpolation, but then again, you might not, because your @alpha isn't inside a string. If that doesn't work, give this a try:

@ievalue:`Math.floor(@{alpha} * 255).toString(16)`;

That one looks funny, I think it's wrong. Hopefully the first one works. But it looks like you will need to use interpolation where you later do this:

"...#@ievalue+@myred+@mygreen+@myblue..."

Because you are inside a string on the last line of your example code, I think that should be:

"...#@{ievalue}+@{myred}+@{mygreen}+@{myblue}..."

As for your latest question, you probably need to use this other bit called Escaping:

Escaping

Sometimes you might need to output a CSS value which is either not valid CSS syntax, or uses proprietary syntax which LESS doesn’t recognize.

To output such value, we place it inside a string prefixed with ~, for example:

.class {
  filter: ~"ms:alwaysHasItsOwnSyntax.For.Stuff()";
}

So your last two lines should probably be this:

filter: ~"progid:DXImageTransform.Microsoft.gradient(startColorstr=#@{ievalue}+@{myred}+@{mygreen}+@{myblue}, endColorstr=#@{ievalue}+@{myred}+@{mygreen}+@{myblue})";

/* For IE 8*/

-ms-filter: ~"progid:DXImageTransform.Microsoft.gradient(startColorstr=#@{ievalue}+@{myred}+@{mygreen}+@{myblue}, endColorstr=#@{ievalue}+@{myred}+@{mygreen}+@{myblue})";

I found this very brief example to back it up, but again, I haven't tried it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!