CSS3D之 perspective、perspective-origin属性

在上一篇博客CSS3 3D相册一文中可以看到,实现该3D效果的关键在于:

  • 利用transform改变各个元素位置
  • 利用transition增加动态效果
  • 利用perspective属性改变视角

transformtransition两个属性都比较好理解,只是perspective视角问题在上一篇博客中没有详细介绍,所以今天就来介绍下perspective以及perspective-origin属性。

1 perspective

perspective指的是:Z平面距离用户之间的距离,浏览器通过该距离来计算用户的视角大小,从而制造出近大远小的3D效果。当元素的z-index大于0或者元素在Z轴正向移动后,元素的大小都会超过实际大小;同理,当元素的z-index小于0或者在Z轴反向移动后,元素大小都会小于实际大小。

接下来我们来看一个实际的例子:

<html>
  <head>
            <style>
            .pers250 {
                perspective: 250px;
            }

            /* Define the container div, the cube div, and a generic face */
            .container {
                width: 100px;
                height: 100px;
                margin: 50px auto;
                border: none;
            }

            .cube {
                width: 100%;
                height: 100%;
                backface-visibility: visible;
                transform-style: preserve-3d;
            }

            .face {
                display: block;
                position: absolute;
                width: 100px;
                height: 100px;
                border: none;
                line-height: 100px;
                font-family: sans-serif;
                font-size: 60px;
                color: white;
                text-align: center;
            }

            /* Define each face based on direction */
            .front {
                background: rgba(0, 0, 0, 0.3);
                transform: translateZ(50px);
            }

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

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

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

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

            .bottom {
                background: rgba(196, 0, 196, 0.7);
                transform: rotateX(-90deg) translateZ(50px);
            }
        </style>
  </head>
  <body>
    <div class="container">
      <div class="cube pers250">
        <div class="face front">1</div>
        <div class="face back">2</div>
        <div class="face right">3</div>
        <div class="face left">4</div>
        <div class="face top">5</div>
        <div class="face bottom">6</div>
      </div>
    </div>
  </body>
</html>

perspective1.png

可以看到:

  • front元素因为朝着Z轴正方向移动,所以其大小会比设定的大小(100px)更大

    perspective2.png

  • back元素朝着Z轴反方向移动,所以其大小会比设定的大小更小

    perspective3.png

  • 其他侧面元素则出现近大远小效果

接下来我们再来改变perspective的大小,看下在不同perspective下会出现哪些变化:

我们首先添加两个新的元素:pers350和pers500

<style>
  .pers350 {
    perspective: 350px;
  }

  .pers500 {
    perspective: 500px;
  }
</style>

<html>
  <div class="container">
    <div class="cube pers250">
      <div class="face front">1</div>
      <div class="face back">2</div>
      <div class="face right">3</div>
      <div class="face left">4</div>
      <div class="face top">5</div>
      <div class="face bottom">6</div>
    </div>
  </div>

  <div class="container">
    <div class="cube pers350">
      <div class="face front">1</div>
      <div class="face back">2</div>
      <div class="face right">3</div>
      <div class="face left">4</div>
      <div class="face top">5</div>
      <div class="face bottom">6</div>
    </div>
  </div>

  <div class="container">
    <div class="cube pers500">
      <div class="face front">1</div>
      <div class="face back">2</div>
      <div class="face right">3</div>
      <div class="face left">4</div>
      <div class="face top">5</div>
      <div class="face bottom">6</div>
    </div>
  </div>
</html>

perspective4.png

可以看到,随着perspective的值增大,页面元素变得更小:这个其实不难理解,相当于用户眼睛离屏幕更远了,元素当然也就显得更小了。

2 perspective-origin

搞清楚了perspective,接下来我们来看下perspective-origin属性:

The perspective-origin CSS property determines the position at which the viewer is looking. It is used as the vanishing point by the perspective property.

perspective-origin定义了观察者的视角相对于显示元素的位置。通常我们用该属性来定义视线灭点,也即视线消失的位置。上面的例子中,我们并未指定perspective-origin属性,因而使用默认的perspective-origin:即元素中心位置,所以每一个元素看起来都是对称的,仿佛我们正对它们。

利用perspective-origin属性,我们可以模拟从不同位置来观察物体的效果:

<div style="margin: 50px;">
  <div class="container" style="margin:50px; display:inline-block; ">
    <div class="cube pers250" style="perspective-origin:left top;">
      <div class="face front">1</div>
      <div class="face back">2</div>
      <div class="face right">3</div>
      <div class="face left">4</div>
      <div class="face top">5</div>
      <div class="face bottom">6</div>
    </div>
  </div>

  <div class="container" style=" margin:50px; display:inline-block;">
    <div class="cube pers250" style="perspective-origin:right bottom;">
      <div class="face front">1</div>
      <div class="face back">2</div>
      <div class="face right">3</div>
      <div class="face left">4</div>
      <div class="face top">5</div>
      <div class="face bottom">6</div>
    </div>
  </div>
</div>

perspective-origin1.png

可以看到,设置perspective-origin属性后,两个元素呈现出完全不同的效果:

  • 左侧元素的perspective-origin值为perspective-origin:left top;,也即其视线灭点为左上角,所以出来的效果类似于从该元素的右下角进行观察
  • 右侧元素的perspective-origin值为right bottom,也即视线灭点为右下角,所以出来的效果类似于从元素的左上角进行观察

这些perspective-origin的含义也就清晰明了了。

perspective-origin的取值总共有以下几种:

  • x-axis
    • left
    • center
    • right
    • length
    • %
  • y-axis
    • top
    • center
    • bottom
    • length
    • %
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值