无法使用 calc() CSS 添加两个变量。

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

Why can't I add two variables with calc() CSS?

问题

我正在尝试创建一个圆形进度条,其中变量可以通过内联样式进行控制。进度条将有两个不同的条,它们应该相加在一起,但仍然是不同的颜色。

这些变量单独使用时是有效的:

.card svg circle {
  width: 100%;
  height: 100%;
  fill: none;
  stroke: #f0f0f0;
  stroke-width: 10;
  stroke-linecap: round;
  
  --laresult: calc(625px - (625px * var(--lapercent)) / 34);
  --saresult: calc(625px - (625px * var(--sapercent)) / 34);
}

.card svg circle.la { 
  stroke-dasharray: 625px;
  stroke-dashoffset: var(--laresult);
  stroke: blue; 
}

.card svg circle.sa {
  stroke-dasharray: 625px;
  stroke-dashoffset: var(--saresult); 
  stroke: pink; 
}

但是,我不希望这两条线重叠,我希望它们看起来像叠加在一起。所以,对于第二个进度条,我想将这两个值相加。然而,当我尝试相加它们时,它就会出错:

.card svg circle {
  width: 100%;
  height: 100%;
  fill: none;
  stroke: #f0f0f0;
  stroke-width: 10;
  stroke-linecap: round;
  
  --laresult: calc(625px - (625px * var(--lapercent)) / 34);
  --saresult: calc(625px - (625px * var(--sapercent)) / 34);
}

.card svg circle.la { 
  stroke-dasharray: 625px;
  stroke-dashoffset: var(--laresult);
  stroke: blue; 
}

.card svg circle.sa {
  stroke-dasharray: 625px;
  stroke-dashoffset: calc(var(--saresult) + var(--laresult)); 
  stroke: pink; 
}

Codepen链接:https://codepen.io/camilapaleno/pen/WNaggpb

英文:

I'm trying to create a circular progress bar where the variables can be controlled with an inline style. The progress bar will have two different bars that should be added together but are still different colors.

These variables work on their own:

.card svg circle {
  width: 100%;
  height: 100%;
  fill: none;
  stroke: #f0f0f0;
  stroke-width: 10;
  stroke-linecap: round;
  
  --laresult: calc(625px - (625px * var(--lapercent)) / 34);
  --saresult: calc(625px - (625px * var(--sapercent)) / 34);
}

.card svg circle.la { 
  stroke-dasharray: 625px;
  stroke-dashoffset: var(--laresult);
  stroke: blue; 
}

.card svg circle.sa {
  stroke-dasharray: 625px;
  stroke-dashoffset: var(--saresult); 
  stroke: pink; 
}

But I don't want the lines to overlap, I want them to appear like they're stacked. So, For the second progress bar, I'd like to add the two together. However, it breaks when I try to add them:

.card svg circle {
  width: 100%;
  height: 100%;
  fill: none;
  stroke: #f0f0f0;
  stroke-width: 10;
  stroke-linecap: round;
  
  --laresult: calc(625px - (625px * var(--lapercent)) / 34);
  --saresult: calc(625px - (625px * var(--sapercent)) / 34);
}

.card svg circle.la { 
  stroke-dasharray: 625px;
  stroke-dashoffset: var(--laresult);
  stroke: blue; 
}

.card svg circle.sa {
  stroke-dasharray: 625px;
  stroke-dashoffset: calc(var(--saresult) + var(--laresult)); 
  stroke: pink; 
}

Codepen: https://codepen.io/camilapaleno/pen/WNaggpb

答案1

得分: 1

在代码中,我看到HTML中的变量在每个元素中都有定义。问题在于,需要使用这两个变量的元素无法“看到”另一个变量的值,因为它在作用域之外(--laresult 无法计算,因为未定义 --lapercent)。在检查器中可见,它是未定义的。

如果你在父元素中这样定义变量...

<svg style="--lapercent: 12; --sapercent: 5">
      <circle cx="105" cy="105" r="100"></circle>
      <circle class="la" cx="105" cy="105" r="100"></circle>
      <circle class="sa" cx="105" cy="105" r="100"></circle>
</svg>

...则变量会被视为已定义。

英文:

Seeing the HTML in the codepen I see the variables are defined in each element:

&lt;svg&gt;
      &lt;circle cx=&quot;105&quot; cy=&quot;105&quot; r=&quot;100&quot;&gt;&lt;/circle&gt;
      &lt;circle class=&quot;la&quot; cx=&quot;105&quot; cy=&quot;105&quot; r=&quot;100&quot; style=&quot;--lapercent: 12&quot;&gt;&lt;/circle&gt;
      &lt;circle class=&quot;sa&quot; cx=&quot;105&quot; cy=&quot;105&quot; r=&quot;100&quot; style=&quot;--sapercent: 5&quot;&gt;&lt;/circle&gt;
&lt;/svg&gt;

The problem I think is that the element that needs both variables has no way to "see" the value of the other variable because it is outside the scope (--laresult can't be calculated because --lapercent is not defined). In the inspector, it is visible that it is not defined:

无法使用 calc() CSS 添加两个变量。

If you define the variables in a parent element this way...

&lt;svg style=&quot;--lapercent: 12; --sapercent: 5&quot;&gt;
      &lt;circle cx=&quot;105&quot; cy=&quot;105&quot; r=&quot;100&quot;&gt;&lt;/circle&gt;
      &lt;circle class=&quot;la&quot; cx=&quot;105&quot; cy=&quot;105&quot; r=&quot;100&quot; &gt;&lt;/circle&gt;
      &lt;circle class=&quot;sa&quot; cx=&quot;105&quot; cy=&quot;105&quot; r=&quot;100&quot;&gt;&lt;/circle&gt;
&lt;/svg&gt;

... the variable appears as defined:

无法使用 calc() CSS 添加两个变量。

答案2

得分: 0

我解决了这个问题。有两个问题:

就像Temani Afif所说,添加这两个变量本来就是不正确的。我需要一个新的公式。

  --laresult: calc(625px - (625px * var(--lapercent)) / 34);
  --saresult: calc((625px - (625px * (var(--sapercent) + var(--lapercent))) / 34));

第二个问题是进度条无法访问第一个变量,因为我只在第一个进度条中内联声明了它。在两个进度条中都声明它们就可以了:

&lt;circle class=&quot;sa&quot; cx=&quot;105&quot; cy=&quot;105&quot; r=&quot;100&quot; style=&quot;--lapercent: 12; --sapercent: 5&quot;&gt;&lt;/circle&gt;
&lt;circle class=&quot;la&quot; cx=&quot;105&quot; cy=&quot;105&quot; r=&quot;100&quot; style=&quot;--lapercent: 12; --sapercent: 5&quot;&gt;&lt;/circle&gt;
英文:

I figured this out. There were two issues:

Like Temani Afif said, adding the two variables was incorrect anyways. I needed a new formula.

  --laresult: calc(625px - (625px * var(--lapercent)) / 34);
  --saresult: calc((625px - (625px * (var(--sapercent) + var(--lapercent))) / 34));

The second issue was that progress bar didn't have access to the first variable since I only declared it inline in the first progress bar. Declaring both in both progress bars worked:

&lt;circle class=&quot;sa&quot; cx=&quot;105&quot; cy=&quot;105&quot; r=&quot;100&quot; style=&quot;--lapercent: 12; --sapercent: 5&quot;&gt;&lt;/circle&gt;
&lt;circle class=&quot;la&quot; cx=&quot;105&quot; cy=&quot;105&quot; r=&quot;100&quot; style=&quot;--lapercent: 12; --sapercent: 5&quot;&gt;&lt;/circle&gt;

huangapple
  • 本文由 发表于 2023年5月17日 22:00:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76272952.html
匿名

发表评论

匿名网友

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

确定