perspective()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
perspective()
CSS 函数用于定义一个变换,设置用户与 z=0 平面之间的距离,即二维界面变为三维时用户的观看视角。其结果是一个 <transform-function>
数据类型。
尝试一下
transform: perspective(0);
transform: perspective(none);
transform: perspective(800px);
transform: perspective(23rem);
transform: perspective(6.5cm);
<section class="default-example" id="default-example">
<div class="transition-all" id="example-element">
<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>
</section>
#default-example {
background: linear-gradient(skyblue, khaki);
perspective: 800px;
perspective-origin: 150% 150%;
}
#example-element {
width: 100px;
height: 100px;
perspective: 550px;
transform-style: preserve-3d;
}
.face {
display: flex;
align-items: center;
justify-content: center;
width: 100%;
height: 100%;
position: absolute;
backface-visibility: inherit;
font-size: 60px;
color: white;
}
.front {
background: rgba(90, 90, 90, 0.7);
transform: translateZ(50px);
}
.back {
background: rgba(0, 210, 0, 0.7);
transform: rotateY(180deg) translateZ(50px);
}
.right {
background: rgba(210, 0, 0, 0.7);
transform: rotateY(90deg) translateZ(50px);
}
.left {
background: rgba(0, 0, 210, 0.7);
transform: rotateY(-90deg) translateZ(50px);
}
.top {
background: rgba(210, 210, 0, 0.7);
transform: rotateX(90deg) translateZ(50px);
}
.bottom {
background: rgba(210, 0, 210, 0.7);
transform: rotateX(-90deg) translateZ(50px);
}
perspective()
变换函数是应用于被变换元素的 transform
值的一部分。这与 perspective
和 perspective-origin
属性不同,后者是附加到在三维空间中变换的子元素的父元素上的。
语法
perspective()
使用的视距由一个 <length>
值指定,表示用户与 z=0 平面之间的距离,或者使用 none
。z=0 平面是所有内容在二维视图中显示的平面,即屏幕。负值会导致语法错误。小于 1px
的值(包括零)将被钳制为 1px
。除 none
外的值会导致 z 值为正的元素看起来更大,z 值为负的元素看起来更小。z 值等于或大于视距值的元素将消失,就好像它们在用户身后一样。较大的视距值表示较小的变换;较小的 perspective()
值表示较大的变换;perspective(none)
表示从无限远的视角观察,没有变换。
perspective(d)
参数
形式语法
<perspective()> =
perspective( [ <length [0,∞]> | none ] )
示例
HTML
<p>没有 perspective:</p>
<div class="no-perspective-box">
<div class="face front">A</div>
<div class="face top">B</div>
<div class="face left">C</div>
</div>
<p>具有 perspective (9cm):</p>
<div class="perspective-box-far">
<div class="face front">A</div>
<div class="face top">B</div>
<div class="face left">C</div>
</div>
<p>具有 perspective (4cm):</p>
<div class="perspective-box-closer">
<div class="face front">A</div>
<div class="face top">B</div>
<div class="face left">C</div>
</div>
CSS
.face {
position: absolute;
width: 100px;
height: 100px;
line-height: 100px;
font-size: 100px;
text-align: center;
}
p + div {
width: 100px;
height: 100px;
transform-style: preserve-3d;
margin-left: 100px;
}
.no-perspective-box {
transform: rotateX(-15deg) rotateY(30deg);
}
.perspective-box-far {
transform: perspective(9cm) rotateX(-15deg) rotateY(30deg);
}
.perspective-box-closer {
transform: perspective(4cm) rotateX(-15deg) rotateY(30deg);
}
.top {
background-color: skyblue;
transform: rotateX(90deg) translate3d(0, 0, 50px);
}
.left {
background-color: pink;
transform: rotateY(-90deg) translate3d(0, 0, 50px);
}
.front {
background-color: limegreen;
transform: translate3d(0, 0, 50px);
}
结果
规范
Specification |
---|
CSS Transforms Module Level 2 # funcdef-perspective |
浏览器兼容性
参见
transform
<transform-function>
- 单独的变换属性: