强制SASS做数学[重复](Force SASS to do math [duplicate])

编程入门 行业动态 更新时间:2024-10-26 02:32:56
强制SASS做数学[重复](Force SASS to do math [duplicate])

这个问题在这里已有答案:

数学与插值变量? 1个答案

试着在我的循环中做数学。

@for $i from 1 through length($colors) { ul li:nth-child(#{$i}) { z-index: length($colors) - #{$i}; } }

但是我在CSS中得到的是:

li:nth-child(2) { z-index: 8-2; }

我怎样才能强迫SASS做数学并得到:

li:nth-child(2) { z-index: 6; }

感谢帮助!

This question already has an answer here:

Math with interpolated variables? 1 answer

Trying to do math inside my loop.

@for $i from 1 through length($colors) { ul li:nth-child(#{$i}) { z-index: length($colors) - #{$i}; } }

But what I get in CSS, for example, is:

li:nth-child(2) { z-index: 8-2; }

How can I force SASS to do math and get:

li:nth-child(2) { z-index: 6; }

Thanks for help!

最满意答案

您将$ i值作为字符串放在z-index属性中。 如果要正确计算值,您应该这样做:

$length: length($colors) @for $i from 1 through length($colors) { ul li:nth-child(#{$i}) { z-index: $length - $i; } }

另外,我建议您设置一个长度值的变量,以防止多次调用此函数。

问候。

You are putting the $i value as a string in the z-index property. If you want to calculate the value correctly you should do it:

$length: length($colors) @for $i from 1 through length($colors) { ul li:nth-child(#{$i}) { z-index: $length - $i; } }

Also I recommend you to set a variable with the value of length to prevent multiple calls to this function.

Regards.

更多推荐

本文发布于:2023-04-27 14:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1327210.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数学   SASS   Force   duplicate   math

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!