英文:
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:
这似乎非常简单,但我无法使其正常工作,可能是因为变量转义错误的理解。
我这里有两个变量:i 和 totalServicesLines。
这里有什么问题?
英文:
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");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论