在 less (css) 中连接值,不带空格
所以我试图制作一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一种解决方案虽然有点难看,但可以使用转义字符串:
请注意,为此您需要 less.js v1.1.x。
One solution, although a little ugly, would be to used escaped strings:
Note you need less.js v1.1.x for this.
更简洁的方法是使用
unit
(官方文档) :在您的示例中变为:
文档:
单位(尺寸,单位)
A cleaner approach is to use
unit
(official documentation):which in your example becomes:
Documentation:
unit(dimension, unit)
谢谢云头。
由于 LESS PHP 的转义有点不同,这对我有用:
Thank you cloudhead.
Since the escaping for LESS PHP is a little different, this is what worked for me:
对于任何发现这个关于无空格串联的旧项目的人,LESS #1375(于 2013 年开放,截至 2016 年 1 月未修复)中有一个错误/功能请求描述了该问题。
问题:
输出:
这会增加显示的额外空间。
解决方案可以是使用嵌入式 JavaScript 替换:输出:
不是最好的解决方案,但在我的实例中唯一有效的解决方案想要可读变量而不是 unicode 转义值。
Update: Thanks to seven-phases-max, the proper solution is MUCH simpler.
我将其留在这里,以防 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:
Output:
Which adds extra space to the display.
A solution can be to use an embedded JavaScript replacement:Output:
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.
I'm leaving this here in case the JavaScript option is a useful clue to future discoverers.