使用 LESS Css 中的公式计算背景位置
我需要能够通过采用精灵图像的 x 和 y 位置的函数方法,使用 LESS Css 中的数学公式来计算背景位置。我对此的最佳猜测如下,但可惜这不起作用。
@icon_base: 30px;
@icon: 24px;
/*sets the position of the background based on the x and y position order */
.icon_position(@x: 1, @y: 1) {
@posx: ((#sizes[@icon_base] - #sizes[@icon])/2 + (@x * #sizes[@icon_base])) * -1;
@posy: ((#sizes[@icon_base] - #sizes[@icon])/2 + (@y * #sizes[@icon_base])) * -1;
background-position: @{posx}px @{posy}px;
}
I need to be able to calculate the background position using a math formula in LESS Css via a function method that takes the x and y position of a sprite image. My best guess for this is as follows, but alas this doesn't work.
@icon_base: 30px;
@icon: 24px;
/*sets the position of the background based on the x and y position order */
.icon_position(@x: 1, @y: 1) {
@posx: ((#sizes[@icon_base] - #sizes[@icon])/2 + (@x * #sizes[@icon_base])) * -1;
@posy: ((#sizes[@icon_base] - #sizes[@icon])/2 + (@y * #sizes[@icon_base])) * -1;
background-position: @{posx}px @{posy}px;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你的操作中有些东西是 lesscss 无法理解的。我将您的操作重写为:
它运行时没有错误,默认情况下它输出:
I think you have some things in your operation that are not understood by lesscss. I rewrote your operation as so:
It runs without error and by default it outputs: