在CSS变量赋值中使用两个变量EOF错误。

huangapple go评论51阅读模式
英文:

Using 2 variables in css variable assignment EOF error

问题

Sure, here's the translation of the code part:

root.style.setProperty('--services-box-delay-' + i, totalServicesLines + 's');

And here's the translation of the explanation:

这似乎非常简单,但我无法使其正常工作,可能是因为变量转义错误的理解。

我这里有两个变量:itotalServicesLines
这里有什么问题?

英文:
root.style.setProperty('--services-box-delay-"+ i +"', totalServicesLines + "s");

This is seemingly so simple, but I can’t get it to work due to (probably) variable escaping wrong understanding.

I have two variables here: i and totalServicesLines.
What is wrong here?

答案1

得分: 1

以下是翻译好的部分:

这样做,我认为会起作用:

root.style.setProperty('--services-box-delay-' + i, totalServicesLines + "s");

示例:
如果 i = 1 并且 totalServicesLines = 3
使用您的代码,您将获得 `root.style.setProperty('--services-box-delay-'i'', '3s');`
变量 i 将不会被计算。

相反,如果您在 i 前后添加双引号,它将变成这样:
`root.style.setProperty('--services-box-delay-1', '3s');`。

这些是 JavaScript 变量,而不是 CSS 变量。

您尝试更改的 CSS 属性是 **--services-box-delay-"1"**,其值将为 "3s"。
英文:

Do like this and I think it will work:

root.style.setProperty('--services-box-delay-"'+ i +'"', totalServicesLines + "s");

Example:
If i = 1 and totalServicesLines = 3
With your code, you will get root.style.setProperty('--services-box-delay-"i"', "3s");
The variable i will not be evaluated.

Instead, if you add " before and after i, it will be like this:
root.style.setProperty('--services-box-delay-1', "3s");.

And these are js and not css variables.

The css property that you're trying to chabge is --services-box-delay-"1" which will get "3s" as value.

答案2

得分: 0

这部分翻译为:

root.style.setProperty(' --services-box-delay- ' + i + '', totalServicesLines + "s");

英文:

Looks weird, but on base of what Mohammed suggested this one works:

root.style.setProperty('--services-box-delay-'+ i +'', totalServicesLines + "s");

huangapple
  • 本文由 发表于 2023年5月13日 15:45:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76241629.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定