在 less (css) 中连接值,不带空格

发布于 2024-11-07 20:26:22 字数 699 浏览 0 评论 0原文

所以我试图制作一个 LESS mixin,它需要一个数字(旋转度数)并输出正确的 css 来旋转元素。问题是,我无法找到为 IE 同时编写“270deg”和“3”(270/90)的方法。以下是我尝试过的事情:

.rotate(@rotation: 0) {
    @deg: deg;
    -webkit-transform: rotate(@rotation deg); // i can see why this doesn't work
    -moz-transform: rotate((@rotation)deg); // parens
    -o-transform: rotate(@rotation+deg); // variable-keyword concatenation
    transform: rotate(@rotation+@deg); // variable-variable concatenation

    // this is the reason I need @rotation to be just a number:
    filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=@rotation/90);
}

.someElement {
    .rotate(270)
}

现在我刚刚修改了编译器脚本,以便它不会在变量/关键字连接之间放置空格。我希望有更好的解决方案。

So I'm trying to make a LESS mixin which takes a number (degrees to rotate) and output the correct css to rotate the element. The problem is, I can't figure out a way to write both "270deg" and "3" (270/90) for IE. Here are the things i've tried:

.rotate(@rotation: 0) {
    @deg: deg;
    -webkit-transform: rotate(@rotation deg); // i can see why this doesn't work
    -moz-transform: rotate((@rotation)deg); // parens
    -o-transform: rotate(@rotation+deg); // variable-keyword concatenation
    transform: rotate(@rotation+@deg); // variable-variable concatenation

    // this is the reason I need @rotation to be just a number:
    filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=@rotation/90);
}

.someElement {
    .rotate(270)
}

For now i've just modified the compiler script so that it doesn't put a space between variable/keyword concatenation. I'd hope theres a better solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

耳钉梦 2024-11-14 20:26:22

一种解决方案虽然有点难看,但可以使用转义字符串:

@degs: ~"@{rotation}deg"
@degs-ie: @rotation / 90;
transform: rotate(@degs);
filter: ~"DXImageBlahBlah(rotation=@{degs-ie})"

请注意,为此您需要 less.js v1.1.x。

One solution, although a little ugly, would be to used escaped strings:

@degs: ~"@{rotation}deg"
@degs-ie: @rotation / 90;
transform: rotate(@degs);
filter: ~"DXImageBlahBlah(rotation=@{degs-ie})"

Note you need less.js v1.1.x for this.

油焖大侠 2024-11-14 20:26:22

更简洁的方法是使用 unit官方文档) :

unit(@rotation, deg)

在您的示例中变为:

transform: rotate(unit(@transition, deg));

文档:

单位(尺寸,单位

  • 尺寸:带有或不带有尺寸单位的数字单位
  • (可选):要更改为的单位,或者如果省略,它将删除该单元

A cleaner approach is to use unit (official documentation):

unit(@rotation, deg)

which in your example becomes:

transform: rotate(unit(@transition, deg));

Documentation:

unit(dimension, unit)

  • dimension: A number, with or without a dimension
  • unit (optional): the unit to change to, or if omitted it will remove the unit
断桥再见 2024-11-14 20:26:22

谢谢云头。
由于 LESS PHP 的转义有点不同,这对我有用:

.rotation(@rotation:90){
  @degs: e("@{rotation}deg");
  @degs-ie: @rotation / 90;

  -webkit-transform: rotate(@degs);
  -moz-transform: rotate(@degs);
  transform: rotate(@degs);
  filter: e("progid:DXImageTransform.Microsoft.BasicImage(rotation=@{degs-ie})");
}

Thank you cloudhead.
Since the escaping for LESS PHP is a little different, this is what worked for me:

.rotation(@rotation:90){
  @degs: e("@{rotation}deg");
  @degs-ie: @rotation / 90;

  -webkit-transform: rotate(@degs);
  -moz-transform: rotate(@degs);
  transform: rotate(@degs);
  filter: e("progid:DXImageTransform.Microsoft.BasicImage(rotation=@{degs-ie})");
}
岁月打碎记忆 2024-11-14 20:26:22

对于任何发现这个关于无空格串联的旧项目的人,LESS #1375(于 2013 年开放,截至 2016 年 1 月未修复)中有一个错误/功能请求描述了该问题。

问题:

@CharTinySpace: \\2009;
content: "@CharTinySpace@CharTinySpace";

输出:

content: " \2009 \2009 ";

这会增加显示的额外空间。

解决方案可以是使用嵌入式 JavaScript 替换:

@CharTinySpace: \\2009;
content: `"@{CharTinySpace}@{CharTinySpace}".replace(/ /g,"")`;

输出:

content: "\2009\2009";

不是最好的解决方案,但在我的实例中唯一有效的解决方案想要可读变量而不是 unicode 转义值。


Update: Thanks to seven-phases-max, the proper solution is MUCH simpler.

@CharTinySpace: \\2009;
content: "@{CharTinySpace}@{CharTinySpace}";

我将其留在这里,以防 JavaScript 选项对未来的发现者来说是一个有用的线索。

For anyone that found this old item about concatenation without spaces, there is a bug/feature request in LESS #1375 (Opened in 2013, unfixed as of Jan 2016) that describes the problem.

Problem:

@CharTinySpace: \\2009;
content: "@CharTinySpace@CharTinySpace";

Output:

content: " \2009 \2009 ";

Which adds extra space to the display.

A solution can be to use an embedded JavaScript replacement:

@CharTinySpace: \\2009;
content: `"@{CharTinySpace}@{CharTinySpace}".replace(/ /g,"")`;

Output:

content: "\2009\2009";

Not the best solution, but the only one that worked in my instance where I want readable varables instead of unicode escape values.


Update: Thanks to seven-phases-max, the proper solution is MUCH simpler.

@CharTinySpace: \\2009;
content: "@{CharTinySpace}@{CharTinySpace}";

I'm leaving this here in case the JavaScript option is a useful clue to future discoverers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文