css - 变换比例时宽度不一致

标签 css css-transforms

为什么下面的两个矩形宽度不同?

第一个是:

  • 1% 宽度
  • 50 倍比例

第二个是:

  • 100% 宽度
  • .5 倍比例

1 * 50 = 50 和 100 * .5 = 50,不是吗?

body {
  margin: 10px 30px;  
}
#box {
  width: 439px;
  height: 200px;
  border: 1px solid black;
  box-sizing: border-box;
}
.line {
  height: 10px;
  transform-origin: left;
}
#example1 {
  width: 1%;
  transform: scaleX(50);
  background-color: red;
}
#example2 {
  width: 100%;
  transform: scaleX(.5);
  background-color: blue;
}
<div id='box'>
  <div id="example1" class="line"></div>
  <div id="example2" class="line"></div>
</div>

最佳答案

至少在 Chrome 中,这种行为似乎是由于百分比值的像素当量的计算方式所致。

  • #example1 的大小是 437px+ 的 1%,正好是 4.37px,但 Chrome(可能还有其他浏览器)似乎将此值舍入到最接近的值完整的号码。由于这种舍入,当您将其放大 50 倍时,宽度仅变为 200 像素。
  • 当您将父级尺寸增加到 459 像素时,您可以看到 #example1 行的宽度变为 250 像素,因为 4.57 会四舍五入为 5 像素。
  • #example2 的大小为 437 像素 (100%),当缩小到 0.5 时,宽度变为 218.5 像素。任何小于 0.5 的值似乎都会向下舍入,而任何等于或大于 0.5 的值都会向上舍入,因此这里宽度变为 219px。
  • 由于上述原因,您可以看到元素宽度之间存在 19px 的差异。在代码片段中,我添加了一些其他带有像素值的行以供引用。绿色的宽度为 218 像素,黄色的宽度为 200 像素,紫色的宽度为 218.4 像素,巧克力色的宽度为 219 像素,棕色的宽度为 218.5 像素。您可以看到绿色和紫色的宽度相同,而巧克力色和棕色的宽度宽 1 像素。
  • 如果将容器宽度设置为 100 的倍数,那么这个问题就会消失。请参阅片段中的第二个 block 。请注意,由于 border 设置为 498px 的 1% 为 5px(因此 50 倍为 250px),但它仍然有一个小偏差,但 498px 的 0.5 倍为 249px。您可以看到,如果删除 border(或“box-sizing”)并将父容器的大小设置为 100 的倍数,它会完美匹配。

+ 437px,因为即使我们说 100%,两侧的 border-width 也会被排除在 box-sizing: border-box 中。

body {
  margin: 10px 30px;
}
#box {
  width: 439px;
  height: 200px;
  border: 1px solid black;
  box-sizing: border-box;
}
.line {
  height: 10px;
  transform-origin: left;
}
#example1 {
  width: 1%;
  transform: scaleX(50);
  background-color: red;
}
#example2 {
  width: 100%;
  transform: scaleX(.5);
  background-color: blue;
}
#example3 {
  width: 218px;
  background-color: green;
}
#example4 {
  width: 200px;
  background-color: yellow;
}
#example5 {
  width: 218.5px;
  background-color: brown;
}
#example6 {
  width: 219px;
  background-color: chocolate;
}
#example7 {
  width: 218.4px;
  background-color: purple;
}
#box2{
  width: 500px;
  height: 200px;
  border: 1px solid black;
  box-sizing: border-box;
}
<div id='box'>
  <div id="example1" class="line"></div>
  <div id="example2" class="line"></div>
  <div id="example3" class="line"></div>
  <div id="example4" class="line"></div>
  <div id="example5" class="line"></div>
  <div id="example6" class="line"></div>  
  <div id="example7" class="line"></div>
</div>

<div id='box2'>
  <div id="example1" class="line"></div>
  <div id="example2" class="line"></div>  
</div>


与我之前提到的相反(在评论和回答中),缩小线末尾的模糊可能不是因为层差异,因为在这两种情况下,线只会获得渲染层而没有图形层。这种模糊是我现在无法解释的。

您可以在以下文章中了解有关图层创建、合成和渲染的更多信息:

关于css - 变换比例时宽度不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31947495/

相关文章:

css - 无法隐藏背面可见性为 : hidden 的元素的背面

html - 我怎么能指定这个类的背景?

html - 在链接上使用 CSS 转换时不稳定

javascript - XML3D css 转换与自定义转换属性

css - IE6 中的下拉菜单插入宽度过大,无法下拉

javascript - 缩放旋转的矩形并找到新的控制点和中心

css - Chrome/Firefox 28+ 中的图像模糊

javascript - 如何使用 float :left and list-style-type in html? 我有一个代码不起作用 float:left 和 list-style-type

html - 为什么这个页脚没有被这些 float 元素取代?

css - 我在更改简单电子邮件表单模块的布局方面遇到障碍,如何自定义此布局?