CSS 属性

CSS属性perspective指定z=0平面和用户之间的距离。它有助于为 3D 定位元素提供一些透视效果。

  • 透视属性的值决定效果的强度。

  • 透视值较大时变换会较小,透视值较小时变换会较大。

  • z>0 的每个 3D 元素都会更大且 z <0>会变小。

  • 当z轴坐标值大于perspective值时,元素无法绘制在三轴上维度空间。

  • 默认情况下,消失点(即观看者/用户正在观看的位置)位于中心。

  • 可以使用 CSS 属性 perspective-origin 更改消失点。

  • 当向此属性传递除无以外的值时,它的作用就像具有位置:固定值的元素的包含块。

属性值

CSS 属性perspective可以具有以下值之一:

1. none:不应用透视变换。

2. <length>:指定从用户到 z=0 平面的距离。

  • 对元素的子元素应用透视变换。

  • 不允许使用负值,并被视为语法错误。

  • 小于 1px 的值被限制为 1px。

适用范围

所有可变形元素。

语法

perspective = none | <length> 

CSS perspective - <length> 值

以下示例演示如何使用 CSS 属性perspective和单位 <length> 的各种值。

<html>
<head>
<style>
   .container {
      width: 150px;
      height: 150px;
      margin: 75px 0 0 75px;
      border: none;
   }

   .cube {
      width: 50%;
      height: 50%;
      perspective-origin: 120% 120%;
      transform-style: preserve-3d;
      padding: 50px;
   }

   .face {
      position: absolute;
      width: 100px;
      height: 100px;
      border: 1px solid black;
      line-height: 80px;
      font-size: 3.5em;
      color: white;
      text-align: center;
   }

   .front {
      background: rgba(178, 0, 178, 0.5);
      transform: translateZ(50px);
   }

   .back {
      background: rgba(178, 178, 0, 0.5);
      color: black;
      transform: rotateY(180deg) translateZ(50px);
   }

   .right {
      background: rgba(0, 0, 178, 0.5);
      transform: rotateY(90deg) translateZ(50px);
   }

   .left {
      background: rgba(178, 0, 0, 0.5);
      transform: rotateY(-90deg) translateZ(50px);
   }

   .top {
      background: rgba(0, 200, 0);
      transform: rotateX(90deg) translateZ(50px);
   }

   .bottom {
      background: rgba(0, 0, 0, 0.2);
      transform: rotateX(-90deg) translateZ(50px);
   } 

   
   .length-cm {
      perspective: 10cm;
   }

   .length-em {
      perspective: 15em;
   }

   .length-px {
      perspective: 200px;
   }

   .length-neg {
      perspective: -100px;
   }
   
</style>
</head>
<body>   
   <h1>perspective</h1>
   
   <div class="container">
   <p>perspective: 10cm</p>
   <div class="cube length-cm">
      <div class="face front"></div>
      <div class="face back"></div>
      <div class="face right"></div>
      <div class="face left"></div>
      <div class="face top"></div>
      <div class="face bottom"></div>
   </div>

   <p>perspective: 15em</p>
   <div class="cube length-em">
      <div class="face front"></div>
      <div class="face back"></div>
      <div class="face right"></div>
      <div class="face left"></div>
      <div class="face top"></div>
      <div class="face bottom"></div>
   </div>
   
   <p>perspective: 200px</p>
   <div class="cube length-px">
      <div class="face front"></div>
      <div class="face back"></div>
      <div class="face right"></div>
      <div class="face left"></div>
      <div class="face top"></div>
      <div class="face bottom"></div>
   </div>
   
   <p>perspective: -100px</p>
   <div class="cube length-neg">
      <div class="face front"></div>
      <div class="face back"></div>
      <div class="face right"></div>
      <div class="face left"></div>
      <div class="face top"></div>
      <div class="face bottom"></div>
   </div>
   </div>   
</body>
</html> 

CSS perspective: none值

以下示例演示了CSS属性perspective使用none值。

<html>
<head>
<style>
   .container {
      width: 150px;
      height: 150px;
      border: none;
   }

   .cube {
      width: 50%;
      height: 50%;
      perspective: none;
      perspective-origin: 120% 120%;
      transform-style: preserve-3d;
   }

   .face {
      position: absolute;
      width: 100px;
      height: 100px;
      border: 1px solid black;
      line-height: 80px;
      font-size: 3.5em;
      color: white;
      text-align: center;
   }

   .front {
      background: rgba(178, 0, 178, 0.5);
      transform: translateZ(50px);
   }

   .back {
      background: rgba(178, 178, 0, 0.5);
      color: black;
      transform: rotateY(180deg) translateZ(50px);
   }

   .right {
      background: rgba(0, 0, 178, 0.5);
      transform: rotateY(90deg) translateZ(50px);
   }

   .left {
      background: rgba(178, 0, 0, 0.5);
      transform: rotateY(-90deg) translateZ(50px);
   }

   .top {
      background: rgba(0, 200, 0);
      transform: rotateX(90deg) translateZ(50px);
   }

   .bottom {
      background: rgba(0, 0, 0, 0.2);
      transform: rotateX(-90deg) translateZ(50px);
   } 
</style>
</head>
<body>   
   <div class="container">
   <p>perspective: none</p>
   <div class="cube">
      <div class="face front"></div>
      <div class="face back"></div>
      <div class="face right"></div>
      <div class="face left"></div>
      <div class="face top"></div>
      <div class="face bottom"></div>
   </div>
   </div>   
</body>
</html>