空间转换
作用:使用transform属性实现元素在空间内的位移、旋转、缩放等效果
一、空间位移
- transform: translate3d(x, y, z);
- transform: translateX(值);
- transform: translateY(值);
- transform: translateZ(值);
1、取值(正负均可)
像素单位数值
百分比
<!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>Document</title>
<style>
body{
perspective: 1000px;
}
.box{
width: 300px;
height: 300px;
background-color: pink;
transform: translate3d(300px,100px,300px);
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
使用注意事项
- transform: translateZ(值),在使用时,默认情况下,无法观察到Z轴位移位置;
- 通过给父级元素使用perspective属性的透视效果 , 空间转换时,为元素添加近大远小、近实远虚的视觉效果 。
- 取值:像素单位数值, 数值一般在800 – 1200。 可取1000px;
- 透视距离也称为视距,所谓的视距就是人的眼睛到屏幕的距离。
二、空间旋转
作用: 使用rotate实现元素空间旋转效果
语法:
- transform: rotateZ(值);
- transform: rotateX(值);
- transform: rotateY(值);
- rotate3d(x, y, z, 角度度数) :用来设置自定义旋转轴的位置及旋转的角度
- x,y,z 取值为0-1之间的数字
-
左手法则
判断旋转方向: 左手握住旋转轴, 拇指指向正值方向, 手指弯曲方向为旋转正值方向 。
<!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>Document</title>
<style>
body{
background-color: #6666;
perspective: 1000px;
}
.box{
margin: 100px auto;
width: 300px;
height: 400px;
background-color: pink;
transition: 10s;
}
.box:hover{
/* transform: rotateX(360deg); */
transform: rotateY(360deg);
}
</style>
</head>
<body>
<div class="box">这是个平面</div>
</body>
</html>
三、空间立体呈现
作用:
使用transform-style: preserve-3d,通过位移,旋转呈现立体图形
用法:
- 父级添加 transform-style: preserve-3d,并赋予宽高。
- 使子元素处于真正的3d空间 ;
- 按需求设置子盒子的位置(位移或旋转)
- 多重转换,注意复合写法,先位移、后旋转
<!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>Document</title>
<style>
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
.box{
position: relative;
margin: 100px auto;
width: 200px;
height: 200px;
background-color: aqua;
transform-style: preserve-3d;
/* transform: rotate3d(1,1,1,30deg); */
transition: all 1s;
perspective: 1000px;
}
.box>div{
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.front{
transform: translateZ(100px);
background-color: red;
}
.back{
transform: translateZ(-100px) ;
background-color:blue;
}
.left{
transform: translateX(-100px) rotateY(-90deg);
background-color: yellow;
}
.right{
transform: translateX(100px) rotateY(90deg);
background-color: violet;
}
.up{
transform: translateY(-100px) rotateX(90deg);
background-color: blueviolet;
}
.bottom{
transform: translateY(100px) rotateX(-90deg);
background-color: tomato;
}
.box:hover{
transform: rotateX(390deg);
}
</style>
</head>
<body>
<div class="box">
<div class="front">前</div>
<div class="back">后</div>
<div class="left">左</div>
<div class="right">右</div>
<div class="up">上</div>
<div class="bottom">下</div>
</div>
</body>
</html>