简体   繁体   English

是否可以运行 javascript 在循环中修改 CSS 变量并使用该变量获得不同的 CSS 结果?

[英]Is it possible to run javascript that modifies a CSS variable in a loop and get different CSS results using that variable?

I'm using PHP to loop through data received from a DB and trying to dynamically affect the width of objects through CSS variables.我正在使用 PHP 循环遍历从数据库接收的数据,并尝试通过 CSS 变量动态影响对象的宽度。 My prototype worked great because it was only using a single iteration and I could get the output looking just the way I wanted.我的原型工作得很好,因为它只使用了一次迭代,我可以让 output 看起来就像我想要的那样。 Once I added the loop I discovered the HTML doesn't appear to be rendered until all HTML is processed.一旦我添加了循环,我发现 HTML 在所有 HTML 被处理之前似乎没有被渲染。 Therefore modifying the CSS variables only provide the last iteration's result for all elements not each element having its own iteration's result.因此,修改 CSS 变量只会为所有元素提供最后一次迭代的结果,而不是每个元素都有自己的迭代结果。

I threw together a quick piece of code to demonstrate my issue.我拼凑了一段快速代码来演示我的问题。 I alert out the CSS values as they change to show they are actually being modified correctly.我提醒 CSS 值,因为它们发生变化以表明它们实际上已被正确修改。

So my question is: Is there a way to force the HTML to render at specific points in the HTML file?所以我的问题是:有没有办法强制 HTML 在 HTML 文件中的特定点呈现? If not is there any way to accomplish what I'm trying to do other than the brute force method of determining and upper limit of my loop and creating a corresponding CSS variable and class for all the possible iterations?如果没有,除了确定循环上限并为所有可能的迭代创建相应的 CSS 变量和 class 的蛮力方法之外,还有什么方法可以完成我正在尝试做的事情吗?

Thanks for your consideration...感谢您的考虑...

 :root { --x: 1; }.wrapper { background-color: lightgreen; width: 200px; height: 200px; text-align: center; }.my-class { background-color: darkgreen; color: white; margin-bottom: 2%; width: calc(100% * var(--x)); height:23%; }
 <script> // Get the root element that holds all the CSS variables var css_root = document.querySelector(':root'); // Function for getting a CSS variable value function GetCssVariable(cssVar) { // Get the styles (properties and values) for the root var root_styles = getComputedStyle(css_root); return root_styles.getPropertyValue(cssVar); } // Function for setting a CSS variable value function SetCssVariable(cssVar, val) { css_root.style.setProperty(cssVar, val); alert(cssVar + " = " + GetCssVariable(cssVar)); // debugging only } </script> <div class="wrapper"> <script>SetCssVariable('--x', 0.2);</script> <div class="my-class">20%</div> <script>SetCssVariable('--x', 0.4);</script> <div class="my-class">40%</div> <script>SetCssVariable('--x', 0.6);</script> <div class="my-class">60%</div> <script>SetCssVariable('--x', 0.8);</script> <div class="my-class">80%</div> </div>

You think too complicated:你想的太复杂了:

 :root { --x: 1; }.wrapper { background-color: lightgreen; width: 200px; height: 200px; text-align: center; }.my-class { background-color: darkgreen; color: white; margin-bottom: 2%; width: calc(100% * var(--x)); height:23%; }
 <div class="wrapper"> <div class="my-class" style="--x: 0.2">20%</div> <div class="my-class" style="--x: 0.4">40%</div> <div class="my-class" style="--x: 0.6">60%</div> <div class="my-class" style="--x: 0.8">80%</div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM