【发布时间】:2016-11-07 10:27:01
【问题描述】:
我正在尝试计算正确的比例以应用于父级内部的子级,但我无法在 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*/
工作示例:
在网址中:MiWeb 具有类的 DIV:class="cs_6c537e46d973809bcbd9abb882d9c7db cssprite" 具有背景图像属性,因为它使用精灵作为源。 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 的实际大小。
【问题讨论】:
标签: css transform scale css-calc