透视和平移Z对角移动

Coo*_*oop 1 css perspective css-transforms

参考此链接:https : //developer.mozilla.org/en-US/docs/Web/CSS/perspective

必须设置透视以沿 z 轴移动子元素。上面的链接显示了不同透视值的示例,所有这些值都将 z 轴设置为对角线方向。

如果我直视 3D 立方体的表面并将其向后移动(沿 z 轴),它看起来会变小(远离我),而不是对角移动。那么为什么 CSS 默认有一个对角线 z 轴呢?有没有办法在 z 轴完全远离用户的情况下使用透视和 ​​translateZ?

我一直在测试的一些代码:

.wrapper {
  position: relative;
  height: 100vh;
  overflow-x: hidden;
  overflow-y: auto;
  perspective: 1px;
  transform-style: preserve-3d;
}

.cube {
  transform-origin: 50% 50%;
  transform: translateZ(-1px);
}

<div class="wrapper">
  <div class="cube"></div>
</div>
Run Code Online (Sandbox Code Playgroud)

Tem*_*fif 7

这完全取决于perspective-origin定义更改应该如何对我们可见。

如果您阅读相同的链接,您会注意到这一点:

默认情况下,消失点位于元素的中心,但可以使用透视原点属性更改其位置。

下面是一些您可以更好地理解的示例:

.wrapper {
  position: relative;
  height: 100px;
  width: 100px;
  border: 1px solid;
  perspective: 10px;
  transform-style: preserve-3d;
}

.cube {
  width: 100%;
  height: 100%;
  background: red;
  animation: change 2s linear infinite alternate;
}

@keyframes change {
  to {
    transform: translateZ(-10px);
  }
}
Run Code Online (Sandbox Code Playgroud)
moving from the center
<div class="wrapper">
  <div class="cube"></div>
</div>
moving from the left
<div class="wrapper" style="perspective-origin:left">
  <div class="cube"></div>
</div>
moving from a custom point
<div class="wrapper" style="perspective-origin:20% 80%">
  <div class="cube"></div>
</div>
Run Code Online (Sandbox Code Playgroud)

当您处理具有width:100%作为位置的默认块元素时,您还需要注意将父元素视为子元素。

删除宽度并查看差异:

.wrapper {
  position: relative;
  border: 1px solid;
  perspective: 10px;
  transform-style: preserve-3d;
}

.cube {
  width: 100px;
  height: 100px;
  background: red;
  animation: change 2s linear infinite alternate;
}

@keyframes change {
  to {
    transform: translateZ(-10px);
  }
}
Run Code Online (Sandbox Code Playgroud)
moving from the center
<div class="wrapper">
  <div class="cube"></div>
</div>
moving from the left
<div class="wrapper" style="perspective-origin:left">
  <div class="cube"></div>
</div>
moving from a custom point
<div class="wrapper" style="perspective-origin:20% 80%">
  <div class="cube"></div>
</div>
Run Code Online (Sandbox Code Playgroud)


在上面的代码中,父容器正在控制透视图。您可以像这样将其移动到子元素:

.wrapper {
  position: relative;
  border: 1px solid;
}

.cube {
  width: 100px;
  height: 100px;
  background: red;
  animation: change 2s linear infinite alternate;
}

@keyframes change {
  to {
    transform: perspective(10px) translateZ(-10px);
  }
}
Run Code Online (Sandbox Code Playgroud)
moving from the center
<div class="wrapper">
  <div class="cube"></div>
</div>
moving from the left
<div class="wrapper" >
  <div class="cube" style="transform-origin:left"></div>
</div>
moving from a custom point
<div class="wrapper" >
  <div class="cube" style="transform-origin:20% 80%"></div>
</div>
Run Code Online (Sandbox Code Playgroud)

如您所见,我们transform-origin使用透视图来控制原点,因为我们使用的是一个变换函数,而不再是一个属性。

改变perspective-origin什么都不会发生

.wrapper {
  position: relative;
  border: 1px solid;
}

.cube {
  width: 100px;
  height: 100px;
  background: red;
  animation: change 2s linear infinite alternate;
}

@keyframes change {
  to {
    transform: perspective(10px) translateZ(-10px);
  }
}
Run Code Online (Sandbox Code Playgroud)
moving from the center
<div class="wrapper">
  <div class="cube"></div>
</div>
moving from the left
<div class="wrapper" style="perspective-origin:left">
  <div class="cube" style="perspective-origin:left"></div>
</div>
moving from a custom point
<div class="wrapper" style="perspective-origin:20% 80%">
  <div class="cube" style="perspective-origin:20% 80%"></div>
</div>
Run Code Online (Sandbox Code Playgroud)