使用数学函数获取整数值
我有一些基本上看起来像这样的 LessCSS:
.foo {
@height: 20px;
@iconHeight: 13px;
background-position: 0 (@height - @iconHeight) / 2;
}
但是,这显然是 background-position: 0 3.5px
,我更喜欢它是一个整数( 的值Math.ceil
),但是这不起作用:
background-position: 0 Math.ceil((@height - @iconHeight) / 2)
使用 javascript 求值运算符也不起作用:
background-position: 0 `Math.ceil((@height - @iconHeight) / 2)`
...因为它被解析为 Math.ceil(20px - 13px)
和 " px" 有语法错误。
我什至尝试过使用 parseInt
0 `Math.ceil((parseInt('@{height}', 10) - parseInt('@{iconHeight}', 10)) / 2)`px
但是结果是这样的:
background-position: 0 4 px
...这是不对的。最后,即使在 JS 评估中添加“px”也不起作用
background-position: 0 `Math.ceil(....) + "px"`;
// becomes
background-position: 0 "4px";
当然有更好的方法!
I have some LessCSS that essentially looks like this:
.foo {
@height: 20px;
@iconHeight: 13px;
background-position: 0 (@height - @iconHeight) / 2;
}
However, this obviously turns out as background-position: 0 3.5px
, and I'd prefer for it to be an integer (the value of Math.ceil
), however this doesn't work:
background-position: 0 Math.ceil((@height - @iconHeight) / 2)
Nor does using the javascript evaluation operator:
background-position: 0 `Math.ceil((@height - @iconHeight) / 2)`
...since this is parsed as Math.ceil(20px - 13px)
and the "px" there is a syntax error.
I've even tried using parseInt
0 `Math.ceil((parseInt('@{height}', 10) - parseInt('@{iconHeight}', 10)) / 2)`px
However this turns out like this:
background-position: 0 4 px
...which isn't right. And finally, even adding the "px" in the JS evaluation doesn't work
background-position: 0 `Math.ceil(....) + "px"`;
// becomes
background-position: 0 "4px";
Surely there's a better way!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用临时变量和
0px
hack 有一个简单的解决方法。There is a simple workaround to the problem using temporary variable and
0px
hack.