我正在尝试计算适用于父母内部孩子的正确比例,但我不能在 transform: scale
CSS 函数中使用 calc()
。我已经用 javascript 完成了这个功能,但我对纯 CSS 解决方案感兴趣,以避免尽可能多地使用脚本使页面膨胀。
示例: CSS
.anyclass{
height:250px; /*this value can change dinamically */
transform:scale(calc(100% / 490 )); /*is using height value*/
}
此定义返回无效的属性值。其他用途,如翻译:
.anyclass{
transform:translate(calc(100% - 50px ));
}
工作没有问题。无论如何我可以使用 calc 仅使用 CSS 计算 div 的正确比例吗?
编辑:为了更好地解释它,在计算中找到一个介于 0 和 1 之间的值不是问题。如果我使用百分比,我只会得到无效的属性值。
测试 1 我尝试使用 CSS3 变量,但在 Scale 函数中没有结果
--height: calc(100% + 0px);
/* height: calc(var(--height) / 490); Calculates the right value: 0.5 */
--soom: calc(var(--height) / 490);
/* height: var(--soom); has the right value: 0.5 */
transform: scale(var(--soom)); /*Does not operate in the value stored in --soom*/
它似乎接受 chrome 中的值,但不操作比例。
ZOOM CSS 属性似乎只能在 chrome 中正常工作。这工作正常。
zoom: calc(100% / 1.490); /* It needs the original value to be divided by 1000 to work properly*/
工作示例:
在 Url: MiWeb 中,具有类:class=“cs_6c537e46d973809bcbd9abb882d9c7db cssprite” 的 DIV 具有 background-image 属性,因为它使用 sprite 作为源。 CSS
background-position: 0 -840px;
width: 800px;
height: 490px;
background-image: url(https://cdn.arturoemilio.es/wp-content/cache/CSS_Sprite/93db60ac3df9134d96d56dd178d4ebf3279fdb39_S.png);
background-repeat: no-repeat;
display: inline-block;
position: initial;
现在原始图像大于可见 div (height:250px aprox)
,我想只使用 CSS 来缩放图像,因为我想避免使用 JS 来更好地兼容被 JS 阻止的浏览器。
我想将该背景图像缩放到正确的大小,正确的值是 250px / 490px ( scale(calc(100% / 490) )。CSS 是在输出之前和 CMS 的其他部分生成的,所以我不能知道生成css规则时父DIV的实际大小。
原文由 Andu 发布,翻译遵循 CC BY-SA 4.0 许可协议