CSS3D之 perspective、perspective-origin属性
来源:互联网 发布:单片机智能反编译器 编辑:程序博客网 时间:2024/05/16 05:56
在上一篇博客CSS3 3D相册
一文中可以看到,实现该3D效果的关键在于:
- 利用transform改变各个元素位置
- 利用transition增加动态效果
- 利用perspective属性改变视角
transform
和transition
两个属性都比较好理解,只是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>
可以看到:
front
元素因为朝着Z
轴正方向移动,所以其大小会比设定的大小(100px)更大back
元素朝着Z
轴反方向移动,所以其大小会比设定的大小更小其他侧面元素则出现近大远小效果
接下来我们再来改变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>
可以看到,随着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-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
- %
- CSS3D之 perspective、perspective-origin属性
- perspective和perspective-origin
- CSS3-转换之perspective-origin
- css3 3d转换之perspective,perspective-origin
- CSS3 perspective-origin
- Perspective
- Perspective
- perspective
- perspective
- CSS3--perspective属性
- CSS3 perspective 属性
- CSS3 perspective属性
- CSS3-perspective 属性
- CSS3 perspective 属性
- css3 perspective属性介绍
- transform的perspective属性
- Eclipse RCP之perspective
- CSS3-转换之perspective
- ubus
- HDU 2063 过山车
- VO、PO、DTO、BO的概念
- AVPicture中data与linesize关系
- Glide的基本使用
- CSS3D之 perspective、perspective-origin属性
- 详解
- recyclerview解析路径json的点击事件
- zentao
- 懒人得多动脑 HNUST 1884
- HDOJ 2011 多项式求和
- C++,OpenCV 中template(模板)的简单理解
- HDU2700-Parity
- 数据结构——二叉树的递归与非递归遍历(先序,中序,后序)