文章目录
一、css3d
1、坐标系认识
<style>
body {
margin: 0;
padding: 0;
background-image: url(../images/grid.png);
}
.box {
width: 200px;
height: 100px;
background-color: skyblue;
/* 形变 */
/* 先位移后选旋转 和 先旋转 在位移他们的坐标系不一样 */
/* transform: translate(200px) rotate(90deg); */
/* transform: rotate(90deg) translate(100px); */
}
</style>
<div class="box">div</div>
2、修改坐标系原点
- 默认坐标系的原点是元素的中间
body {
margin: 0;
padding: 0;
background-image: url(../images/grid.png);
}
.box {
width: 200px;
height: 100px;
background-color: skyblue;
/* transform-origin: bottom; */
transform: rotate(90deg);
/* 下边界的中点 bottom -> 50%, 100% */
/* transform-origin: bottom; */
/* 左边界的中点 left -> 0, 50% */
transform-origin: left;
}
二、3D动画
2.1 3D旋转 rotate
body {
margin: 0;
padding: 0;
background-image: url(../images/grid.png);
}
.box {
width: 200px;
height: 100px;
background-color: skyblue;
/* 形变 */
/* transform: rotateZ(45deg); */
/* 简写 */
/* transform: rotate(45deg); */
/* transform: rotate3d(0, 0, 1, 45deg); */
transform: translate(200px, 100px) rotate3d(1, 0, 0, 45deg);
}
body{
margin: 0;
padding: 100px;
background-image: url(../images/grid.png);
}
.box{
width: 100px;
height: 100px;
background-color: skyblue;
/* 形变 */
transform: rotateX(-33.5deg) rotateY(45deg);
}
2.2 透视
2.1 使用方式①在父元素上定义 CSS 透视属性
body {
margin: 0;
padding: 0;
background-image: url(../images/grid.png);
}
.box {
position: relative;
width: 200px;
height: 100px;
background-color: skyblue;
/* 在父元素添加透视效果 */
perspective: 200px;
}
.item {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: pink;
/* 形变 */
/* 必须是3d的才会发生透视 */
transform: rotateY(60deg);
}
<div class="box">div
<div class="item">10</div>
</div>
2.2 方法②如果它是子元素或单元素子元素,可以使用函数 perspective()
body {
margin: 0;
padding: 0;
background-image: url(../images/grid.png);
}
.box {
position: relative;
width: 200px;
height: 100px;
background-color: skyblue;
/* 在当前元素中直接添加透视效果 */
transform: translate(200px, 100px) perspective(200px) rotateX(45deg)
}
<div class="box">div</div>
2. 3D平移
body {
margin: 0;
padding: 0;
background-image: url(../images/grid.png);
}
.box {
width: 200px;
height: 100px;
background-color: skyblue;
/* 形变 */
/* 可以看称第一个参数为眼睛距离物体的距离,第二个为物体距离眼睛的距离,当第二个数值超过或者等于第二个则是看不到的 */
transform: perspective(300px) translate3d(200px, 100px, 200px);
}
2.4 3D缩放
body {
margin: 0;
padding: 0;
background-image: url(../images/grid.png);
}
.box {
width: 200px;
height: 100px;
background-color: skyblue;
/* 形变 */
transform: scaleX(2) scaleY(2);
/* 简写 */
transform: scale3d(2, 2, 1);
transform-origin: 0 0;
}
2.5 3D空间 - transform-style
body {
margin: 0;
padding: 0;
background-image: url(../images/grid.png);
}
.box {
position: relative;
width: 200px;
height: 100px;
background-color: skyblue;
/* 在父元素中添加 transform-style来启用3D空间 */
transform-style: preserve-3d;
/* 在父元素添加透视效果 */
perspective: 300px;
}
.item {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: pink;
/* 形变 */
transform: rotateX(70deg) translateX(50px);
}
<div class="box">div
<div class="item">10</div>
</div>
2.6 3D空间 - 制作正方体
body{
margin: 0;
padding: 100px;
background-image: url(../images/grid.png);
}
.box{
position: relative;
width: 100px;
height: 100px;
background-color: red;
/* 在父元素中添加 transform-style来启用3D空间 */
transform-style: preserve-3d;
transform: rotateX(-33.5deg) rotateY(45deg) scaleZ(2);
}
.item{
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.top{
background-color: rgba(255, 0, 0, 0.4);
transform: rotateX(90deg) translateZ(50px);
}
.bottom{
background-color: rgba(0, 255, 0, 0.4);
transform: rotateX(-90deg) translateZ(50px);
}
.front{
background-color: rgba(100, 100, 100, 0.4);
transform: rotateY(0deg) translateZ(50px);
}
.back{
background-color: rgba(0, 255, 255, 0.4);
transform: rotateY(-180deg) translateZ(50px);
}
.left{
background-color: rgba(255, 255, 0, 0.4);
transform: rotateY(-90deg) translateZ(50px);
}
.right{
background-color: rgba(0, 0, 255, 0.4);
transform: rotateY(90deg) translateZ(50px);
}
<!-- 父元素(舞台) -->
<div class="box">div
<!-- 子元素 -->
<div class="item top">1</div>
<div class="item bottom">2</div>
<div class="item front">3</div>
<div class="item back">4</div>
<div class="item left">5</div>
<div class="item right">6</div>
</div>
2.7 背部不可见
body {
margin: 0;
padding: 0;
background-image: url(../images/grid.png);
}
.box {
position: relative;
width: 200px;
height: 100px;
background-color: skyblue;
/* 在父元素添加透视效果 */
perspective: 800px;
}
.item {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: pink;
/* 形变 */
/* transform: rotateY(180deg); */
/* 元素背向是否可见 */
backface-visibility: hidden;
/* 帧动画 */
animation: loop 6s linear infinite;
}
@keyframes loop {
0% {
transform: rotateY(0deg);
}
100% {
transform: rotateY(-360deg);
}
}
<div class="box">div
<div class="item">10</div>
</div>
当元素在背面的时候看不到
三、案例
3.1 webpack-logo
<body>
<div class="webpack-logo">
<ul class="cube-inner">
<li class="top">1</li>
<li class="bottom">2</li>
<li class="left">3</li>
<li class="right">4</li>
<li class="front">5</li>
<li class="back">6</li>
</ul>
<ul class="cube-outer">
<li class="top">1</li>
<li class="bottom">2</li>
<li class="left">3</li>
<li class="right">4</li>
<li class="front">5</li>
<li class="back">6</li>
</ul>
</div>
</body>
html,
body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
background-color: #2b3a42;
display: flex;
align-items: center;
justify-content: center;
}
ul {
margin: 0;
padding: 0;
list-style: none;
}
.webpack-logo {
width: 100%;
height: 200px;
position: relative;
}
.cube-inner {
width: 100px;
height: 100px;
position: absolute;
top: 50%;
left: 50%;
/* 不用transform的原因是 */
margin: -50px 0 0 -50px;
/* background-color: #fff; */
/* 开启3D效果 */
transform-style: preserve-3d;
transform: rotateX(-33.5deg) rotateY(45deg);
animation: innerLoop 6s ease-in-out infinite;
}
@keyframes innerLoop {
0% {
transform: rotateX(-33.5deg) rotateY(45deg);
}
50%,
100% {
transform: rotateX(-33.5deg) rotateY(-45deg);
}
}
.cube-inner li {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
border: 1px solid #fff;
background-color: #175d96;
}
.cube-inner .top {
transform: rotateX(90deg) translateZ(50px);
}
.cube-inner .bottom {
transform: rotateX(-90deg) translateZ(50px);
}
.cube-inner .left {
transform: translateZ(50px);
}
.cube-inner .right {
/* background-color: #fff; */
transform: rotate(-180deg) translateZ(-50px);
}
.cube-inner .back {
transform: rotateY(90deg) translateZ(50px);
/* background-color: red; */
}
.cube-inner .front {
transform: rotateY(-90deg) translateZ(50px);
/* background-color: #fff; */
}
.cube-outer {
width: 200px;
height: 200px;
position: absolute;
top: 50%;
left: 50%;
/* 不用transform的原因是 */
margin: -100px 0 0 -100px;
/* background-color: #fff; */
/* 开启3D效果 */
transform-style: preserve-3d;
transform: rotateX(-33.5deg) rotateY(45deg);
animation: outLoop 6s ease-in-out infinite;
}
@keyframes outLoop {
0% {
transform: rotateX(-33.5deg) rotateY(-45deg);
}
50%,
100% {
transform: rotateX(-33.5deg) rotateY(45deg);
}
}
.cube-outer li {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
border: 1px solid #fff;
background-color: rgba(141, 214, 249, 0.5);
}
.cube-outer .top {
transform: rotateX(90deg) translateZ(100px);
}
.cube-outer .bottom {
transform: rotateX(-90deg) translateZ(100px);
}
.cube-outer .left {
transform: translateZ(100px);
}
.cube-outer .right {
/* background-color: #fff; */
transform: rotate(-180deg) translateZ(-100px);
}
.cube-outer .back {
transform: rotateY(90deg) translateZ(100px);
/* background-color: red; */
}
.cube-outer .front {
transform: rotateY(-90deg) translateZ(100px);
/* background-color: #fff; */
}