为什么这个 calc() 函数在变换比例中不起作用?

Why does this calc() function not work in transform scale?

calc() 显然在转换中有效,但这并不:

transform: scale(calc(0.75 + (0.3 - 0.75) * ((100vw - 320px) / (780 - 320))));

此外,这也有效,但它基本上是相同的公式,只是百分比与小数不同。仅添加了此内容,以便您可以在此处查看公式:

right: calc(30% + (75 - 30) * ((100vw - 320px) / (780 - 320)));

例如,我有一个容器,其宽度为百分比,最大宽度为 300 像素。我希望它的子项始终使用 transform:scale() 缩放到父项实际当前宽度的百分比。

为什么我的转换 calc 函数不起作用?使用 Chrome.

100vw - 320px 用于计算 window 宽度的最小值和最大值之间的值。可以在这里做吗?

你有两个问题。第一个是关于没有刻度的公式:

calc(0.75 + (0.3 - 0.75) * ((100vw - 320px) / (780 - 320)))

这是无效的,因为您要添加 number (0.75) 和 length ((0.3 - 0.75) * ((100vw - 320px) / (780 - 320)))

At + or -, check that both sides have the same type, or that one side is a <number> and the other is an <integer>. If both sides are the same type, resolve to that type. If one side is a <number> and the other is an <integer>, resolve to .ref

第二个问题是比例尺只取数字,因此您需要更正公式,通过删除任何类型的单位将第二部分转换为数字 (vw,px,等)。

基本上,你想做的事情不能用这种方式完成,因为你无法将 (100vw - 320px) 转换为数字,除非你考虑使用一些 JS,因为这超出了 CSS。即使使用 JS,您也需要定义将像素数转换为非像素数的逻辑。


rightpercentage 中使用相同的公式可以正常工作,因为:

If percentages are accepted in the context in which the expression is placed, and they are defined to be relative to another type besides <number>, a <percentage-token> is treated as that type. For example, in the width property, percentages have the <length> type. A percentage only has the <percentage> type if in that context <percentage> values are not used-value compatible with any other type. If percentages are not normally allowed in place of the calc(), then a calc() expression containing percentages is invalid in that context.ref

所以在这种情况下,百分比允许与 right 一起使用,因为我们可以解决它,因此 forumla 将有效,因为最后它会像 A% + Bpx.