三维坐标系
简称立体坐标系 比二维坐标系多一个Z轴
3D位移
3D位移在2D位移的基础上多一个可移动的Z轴
transform: translate3d(x,y,z);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>3D位移</title>
<style>
* {
margin: 0;
padding: 0;
}
div {
width: 200px;
height: 200px;
background-color: purple;
transform: translate3d(600px,100px,100px);
}
</style>
</head>
<body>
<div></div>
</body>
</html>
注意事项
- x轴 y轴一般用px及百分比当做单位
- z轴一般用px当做单位,必须借助透视功能实现3D;
- z轴向外移动一般是正值,向内移动是负值。
perspective
特点
- 透视是视距,便是人的眼睛到屏幕的距离,单位为px
- 透视写在被观察元素的父元素上。
- 透视的单位越大,看到的物品就越小,透视的单位越小,看到的物品就越大。
- 在透视固定的情况下,z轴越大,看到的物品就越大,z轴越小,看到的物品就越小。
在这张图中,d为透视,位于人的眼睛和被观察物体的中间
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>perspective</title>
<style>
body {
perspective:500px
}
div {
width: 200px;
height: 200px;
background:pink;
transform: translate3d(600px,100px,100px);
}
</style>
</head>
<body>
<div></div>
</body>
</html>
translateZ
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>translateZ</title>
<style>
* {
margin: 0;
padding: 0;
}
body {
/*透视写在被观察元素的父元素上*/
perspective: 500px;
}
div {
width: 100px;
height: 100px;
background-color: purple;
margin: 200px auto;
transform: translateZ(200px);
}
</style>
</head>
<body>
<div></div>
<div></div>
</body>
</html>
3D旋转
3D 旋转可以让元素在三维平面内沿着 x 轴、y 轴、z 轴 或者自定义轴进行旋转
rotateX
左手弯曲的手指方向即为x轴的正方向
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>rotateX</title>
<style>
* {
margin: 0;
padding: 0;
}
body {
/*实现3D功能 必须借助透视*/
perspective: 200px;
}
img {
display: block;
width: 400px;
margin: 200px auto;
transition: all 2s linear 0s;
}
img:hover {
transform: rotateX(45deg);
}
</style>
</head>
<body>
<img src="./images/desktop.jpg" alt="">
</body>
</html>
rotateY
左手手指弯曲的方向即为y轴的正方向
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>rotateY</title>
<style>
* {
margin: 0;
padding: 0;
}
body {
/*实现3D功能 必须借助透视*/
perspective: 200px;
}
img {
display: block;
width: 400px;
margin: 200px auto;
transition: all 2s linear 0s;
}
img:hover {
transform: rotateY(45deg);
}
</style>
</head>
<body>
<img src="./images/desktop.jpg" alt="">
</body>
</html>
rotateZ
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>rotateZ</title>
<style>
* {
margin: 0;
padding: 0;
}
body {
/*实现3D功能 必须借助透视*/
perspective: 200px;
}
img {
display: block;
width: 400px;
margin: 200px auto;
transition: all 2s linear 0s;
}
img:hover {
transform: rotateZ(45deg);
}
</style>
</head>
<body>
<img src="./images/desktop.jpg" alt="">
</body>
</html>
rotate3d
transform: rotate3d(x, y, z, deg)
– 沿着自定义轴旋转 deg 为角度transform: rotate3d(1, 1, 0, 45deg)
– 沿着对角线旋转 45degtransform: rotate3d(1, 0, 0, 45deg)
– 沿着 x 轴旋转 45deg
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>rotate3D</title>
<style>
* {
margin: 0;
padding: 0;
}
img {
display: block;
width: 600px;
margin: 200px auto;
transition: all 2s linear 0s;
}
img:hover {
/*对角线旋转45deg*/
transform: rotate3d(1,1,0,45deg);
}
</style>
</head>
<body>
<img src="./images/desktop.jpg" alt="">
</body>
</html>
transform-style
transform-style;preserve-3d 让子元素保持3d空间环境
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>transform-style</title>
<style>
* {
margin: 0;
padding: 0;
}
body {
perspective: 600px;
}
@keyframes scale {
0% {
transform: rotateX(0deg);
}
100% {
transform: rotateX(360deg);
}
}
.box {
position: relative;
width: 500px;
height: 500px;
margin: 200px auto;
transform-style: preserve-3d;
background-color:purple;
animation: scale 2s linear 0s infinite;
}
.box div:nth-of-type(n){
position: absolute;
top: 0;
left: 0;
width: 500px;
height: 500px;
}
.box div:nth-of-type(1) {
background-color: yellow;
transform: rotateX(120deg);
}
.box div:nth-of-type(2) {
background-color: green;
transform: rotateX(240deg);
}
</style>
</head>
<body>
<div class="box">
<div></div>
<div></div>
</div>
</body>
</html>
案例之演示
两面翻转的盒子
- 先准备一个大盒子 里面装着两个小盒子
- 让其中的一个盒子翻转180deg
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>两面反转的盒子</title>
<style>
* {
margin: 0;
padding: 0;
}
body {
perspective: 500px;
}
.box {
position: relative;
width: 500px;
height: 500px;
margin: 200px auto;
/* 子元素保持3D空间*/
transform-style: preserve-3d;
transition: all 0.5s linear 0s;
}
.box:hover {
transform: rotateY(180deg);
}
.front,
.back {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
text-align: center;
line-height: 500px;
border-radius: 50%;
}
.front {
background-color: purple;
z-index: 5;
}
.back {
background-color: yellow;
transform: rotateY(180deg);
}
</style>
</head>
<body>
<div class="box">
<div class="front">尧子陌</div>
<div class="back">临风笑却世间</div>
</div>
</body>
</html>
3D导航栏案例
当有transform属性时,位移放在最前面,切不能改变顺序.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>3D导航栏</title>
<style>
* {
margin: 0;
padding: 0;
}
ul {
/* 清除浮动 */
width: 1200px;
margin: 200px auto;
}
li {
float: left;
list-style: none;
width: 180px;
height: 50px;
margin: 20px;
/* 透视 */
perspective: 500px;
}
.box {
position: relative;
width: 100%;
height: 100%;
/* 子元素开启3D空间 */
transform-style: preserve-3d;
/* 开启过渡 */
transition: all 2s linear 0s;
}
.box:hover {
transform: rotateX(90deg);
}
.front,
.back {
position: absolute;
width: 100%;
height: 100%;
text-align: center;
line-height: 50px;
}
.front {
background-color: yellow;
/* z轴正方向移动25px */
transform: translateZ(25px);
}
.back {
background-color: blue;
/* 先向下移动25px 再延x轴反方向旋转90deg */
transform: translateY(25px) rotateX(-90deg);
}
</style>
</head>
<body>
<ul>
<li>
<div class="box">
<div class="front"></div>
<div class="back"></div>
</div>
</li>
<li>
<div class="box">
<div class="front"></div>
<div class="back"></div>
</div>
</li>
<li>
<div class="box">
<div class="front"></div>
<div class="back"></div>
</div>
</li>
<li>
<div class="box">
<div class="front"></div>
<div class="back"></div>
</div>
</li>
<li>
<div class="box">
<div class="front"></div>
<div class="back"></div>
</div>
</li>
</ul>
</body>
</html>
3D木马
思路
- 分析如下:给body设置透视 给div的父元素section设置开启3d效果
- 把每个div盒子摆放到正确的位置 利用动画即可
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>旋转木马</title>
<style>
* {
margin: 0;
padding: 0;
}
body {
/* 透视 */
perspective: 500px;
}
/* 定义动画 */
@keyframes rotate {
0% {
transform: rotateY(0deg);
}
100% {
transform: rotateY(360deg);
}
}
section {
position: relative;
width: 200px;
height: 200px;
margin: 200px auto;
/* 开启3D效果 */
transform-style: preserve-3d;
/* 使用动画 */
animation: rotate 3s linear 0s infinite;
}
section div:nth-of-type(n) {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
div img {
width: 200px;
height: 200px;
}
section div:nth-of-type(1) {
transform: rotateY(0deg) translateZ(300px);
}
section div:nth-of-type(2) {
transform: rotateY(60deg) translateZ(300px);
}
section div:nth-of-type(3) {
transform: rotateY(120deg) translateZ(300px);
}
section div:nth-of-type(4) {
transform: rotateY(180deg) translateZ(300px);
}
section div:nth-of-type(5) {
transform: rotateY(240deg) translateZ(300px);
}
section div:nth-of-type(6) {
transform: rotateY(300deg) translateZ(300px);
}
</style>
</head>
<body>
<section>
<div>
<img src="./images/desktop.jpg" alt="">
</div>
<div>
<img src="./images/desktop.jpg" alt="">
</div>
<div>
<img src="./images/desktop.jpg" alt="">
</div>
<div>
<img src="./images/desktop.jpg" alt="">
</div>
<div>
<img src="./images/desktop.jpg" alt="">
</div>
<div>
<img src="./images/desktop.jpg" alt="">
</div>
</section>
</body>
</html>