1. 3d移动 translate3d
transform: translateX(100px);
transform: translateY(100px);
transform: translateZ(100px);/* 仅仅是在Z轴上移动(注意:translateZ一般用px单位) */
transform: translateX(100px) translateX(100px) translateX(100px);
transform: translate3d(x, y, z);
/* 其中 x、y、z 分别指要移动的轴的方向的距离 */
/* xyz是不能省的,没有就写0 */
2. 3d旋转 rotate3d
transform: rotateX(45deg);
/*沿着x轴正方向旋转 45度*/
transform: rotateY(45deg);
/*沿着y轴正方向旋转 45deg*/
transform: rotateZ(45deg);
/*沿着Z轴正方向旋转 45deg*/
transform: rotate3d(x, y, z, deg);
/* 沿着自定义轴旋转 deg为角度(了解即可)。*/
/* 举例 */
transform: rotate3d(1, 0, 0, 45deg);
/*就是沿着x轴旋转 45deg*/
transform: rotate3d(1, 1, 0, 45deg);
/*就是沿着对角线旋转 45deg*/
旋转方向判断:左手准则(度数为正,则逆时针)。
3. 透视 perspective
perspective: 1000px;
(透视值越大,成像更小。)
4. 3d呈现 transfrom-style
- 控制子元素是否开启三维立体环境。
- 代码写给父级,但是影响的是子盒子。
transform-style: flat;
/* 子元素不开启3D立体空间(默认) */
transform-style: preserve-3d;
/* 子元素开启立体空间 */
4. 案例——两面魔方翻转
4.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>测试区</title>
<style>
.box {
position: relative;
width: 200px;
height: 100px;
margin: 100px auto;
transition: all .6s;
transform-style: preserve-3d;
}
.box:hover {
transform: rotateX(90deg);
}
.front,
.back {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
line-height: 100px;
text-align: center;
font-size: 16px;
color: white;
backface-visibility: hidden;
}
.front {
background-color: pink;
/* 重要!!!应该向前移动front,这样box的旋转中心轴就在立方体中间了 */
transform: translateZ(50px);
}
.back {
background-color: purple;
/* 不能先旋转再移动 */
transform: translateY(50px) rotateX(-90deg);
}
</style>
</head>
<body>
<div class="box">
<div class="front">你好呀</div>
<div class="back">你是谁</div>
</div>
</body>
</html>
4.2 水平方向的旋转
<!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>
body {
perspective: 400px;
}
.box {
position: relative;
transform-style: preserve-3d;
width: 200px;
height: 200px;
margin: 100px auto;
transition: all 2s;
}
.front,
.back {
position: absolute;
top: 0px;
left: 0px;
width: 100%;
height: 100%;
background-color: aquamarine;
font-size: 50px;
line-height: 200px;
text-align: center;
border-radius: 50%;
color: aliceblue;
}
.front {
z-index: 1;
}
.back {
background-color: cornflowerblue;
transform: rotateY(180deg);
}
.box:hover {
transform: rotateY(180deg);
}
</style>
</head>
<body>
<div class="box">
<div class="front">翻转</div>
<div class="back">我再翻转</div>
</div>
</body>
</html>
4.3 旋转木马
<!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>
body {
perspective: 1000px;
}
section {
position: relative;
width: 200px;
height: 200px;
margin: 150px auto;
transform-style: preserve-3d;
animation: rotate 7s linear infinite;
background: url(前端学习\img1.jpg) no-repeat;
}
img {
width: 200px;
height: 200px;
}
section:hover {
animation-play-state: paused;
}
@keyframes rotate {
0% {}
100% {
transform: rotateY(360deg);
}
}
div {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: url(img1.jpg) no-repeat;
}
div:nth-child(1) {
transform: translateZ(300px);
}
div:nth-child(2) {
/* 先旋转再移动 */
/* 这里旋转的时候z轴方向也跟着旋转,因此还是300px */
transform: rotateY(60deg) translateZ(300px);
}
div:nth-child(3) {
transform: rotateY(120deg) translateZ(300px);
}
div:nth-child(4) {
transform: rotateY(180deg) translateZ(300px);
}
div:nth-child(5) {
transform: rotateY(240deg) translateZ(300px);
}
div:nth-child(6) {
transform: rotateY(300deg) translateZ(300px);
}
</style>
</head>
<body>
<section>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</section>
</body>
</html>