如果父元素具有其自身的转换,为什么忽略CSS translateZ? [英] Why CSS translateZ is ignored if parent element has a transform of its own?

查看:67
本文介绍了如果父元素具有其自身的转换,为什么忽略CSS translateZ?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以解释这种行为吗?

Can anyone explain this behavior?

我有三个嵌套元素:一个容器 div ,一个子容器 div 和一个"shape/image" div .标记如下:

I have three nested elements: a container div, a subcontainer div and a "shape/image" div. The markup is like below:

<html>
  <div class="container">
    <div class="subcontainer">
      <div class="shape"></div>
    </div>
  </div>
</html>

当我对形状应用 transform:translateZ()时,显然只有在父母之一应用了某些 perspective 的情况下,该方法才有效.它不必一定是直接父级,它可以是父级的父级 div (容器).当容器具有 perspective 时,形状将沿z方向精细移动.但是,当子容器(父容器)在 transform 中有除 unset 之外的其他内容时,形状中的 translateZ()会被完全忽略.当然,对直接父级应用 perspective 可以使形状中的 translateZ()起作用,但是我想了解父级 transform 中的内容阻止透视图可以通过以下形状看到:

When I apply transform: translateZ() to the shape, obviously it will only work if one of the parents has some perspective applied. It doesn't need to be necessarily the direct parent, it can be the parent's parent div (the container). When the container has the perspective, the shape moves in the z direction fine. But, when the subcontainer (the parent) has something in the transform, other than unset, the translateZ() from the shape is fully ignored. Of course, applying perspective to the direct parent makes the translateZ() in the shape work, but I wanted to understand what in the parent transform blocks the perspective to be seen by the shape:

以下作品:

.container{
  perspective: 1000px;
}

.subcontainer{
  transform: unset;     // <- or if this is not present at all
}

.shape{
  transform: translateZ(300px);
}

以下不起作用:

.container{
  perspective: 1000px;
}

.subcontainer{
  transform: scale(1.001);
}

.shape{
  transform: translateZ(300px);    // <- this is fully ignored
}

这里是中的此示例.

在许多文档中,我读到 perspective 必须是在父元素中设置的属性.但是 MDN 似乎表明实际上,它可以在 transform 规则内的预期元素本身中设置.那么,父级的 perspective:500px 等同于子级的 transform:Perspective(500px)吗?

In many docs I read that perspective must be a property set in the parent element. But MDN seems to show that it can in fact be set in the intended element itself, inside the transform rule. So, is perspective: 500px in the parent equivalent to transform: perspective(500px) in the child?

推荐答案

来自

默认情况下,变换后的元素不会不创建3D渲染上下文,而是创建其内容的展平表示.但是,由于构造共享公共3维空间的已转换对象的层次结构很有用,因此可以通过为 transform-style指定一个 preserve-3d 值来覆盖这种扁平化行为属性.这允许转换后的元素的后代共享相同的3D渲染上下文.

By default, transformed elements do not create a 3D rendering context and create a flattened representation of their content. However, since it is useful to construct hierarchies of transformed objects that share a common 3-dimensional space, this flattening behavior may be overridden by specifying a value of preserve-3d for the transform-style property. This allows descendants of the transformed element to share the same 3D rendering context.

因此,只需将 transform-style:preserve-3d 添加到subcontainer元素,即可尊重祖父母的视角:

So simply add transform-style:preserve-3d to the subcontainer element and the perspective of the grand-parent will be respected:

.container{
  background: #f4f7be;
  height: 100vh;
  width: 100vw;
  display: flex;
  justify-content: center;
  align-items: center;
  perspective: 1000px;
}

.subcontainer{
  background: #baf7f6;
  width: 70%;
  height: 70%;
  display: flex;
  justify-content: center;
  align-items: center;
  transform: scale(1.001);
  transform-style:preserve-3d;
}

.shape{
  background: #555;
  width: 40%;
  height: 50%;
  clip-path: polygon(50% 0%, 0% 100%, 100% 100%);
  transform: translateZ(300px);
}

<html>
  <div class="container">
    <div class="subcontainer">
      <div class="shape"></div>
    </div>
  </div>
</html>

那么,父级中的Perspective:500px等同于子级中的transform:Perspective(500px)吗?

So, is perspective: 500px in the parent equivalent to transform: perspective(500px) in the child?

并不完全等效,因为您需要考虑其他因素,例如父维度,变换来源,透视图来源等

Not exactly equivalent because you need to consider other factors like the parent dimension, transform-origin, perspective-origin, etc

一个简单的例子:

.box {
  padding: 20px;
  border: 1px solid;
}

.box>div {
  width: 100px;
  height: 100px;
  background: red;
}

<div class="box" style="perspective:100px;">
  <div style="transform:translateZ(10px);"></div>
</div>

<div class="box">
  <div style="transform:perspective(100px) translateZ(10px);"></div>
</div>

差异是由原产地引起的.在第一种情况下,透视图将使用父级的中心作为原点(因为 perspective-origin 默认为中心).在第二种情况下,元素的中心将用作原点(因为 transform-origin 默认为中心)

The difference is due to the origin. In the first case, the perspective will use the center of the parent as origin (because perspective-origin is center by default). In the second case, the center of the element will be used as origin (because transform-origin is center by default)

相关问题以获取更多详细信息和示例:

Related questions for more detail and examples:

透视并和平移动Z对角线

为什么在更新某些样式时透视图无法给出相同的结果?

如果在属性的末尾设置了透视图,则CSS 3d转换将不起作用

这篇关于如果父元素具有其自身的转换,为什么忽略CSS translateZ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆