二、空间转换、动画
1、空间转换
- 空间:是从坐标轴定义的 X、Y 和 Z 三条坐标轴构成了一个立体空间,Z 轴位置与视线方向相同。
- 空间转换也叫 3D转换
- 属性:transform
平移
属性
transform: translate3d(x, y, z); transform: translateX(); transform: translateY(); transform: translateZ();
取值(正负均可)
- 像素单位数值
- 百分比(参照盒子自身尺寸计算结果)
示例:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> .box { width: 200px; height: 200px; margin: 100px auto; background-color: pink; transition: all 0.5s; } .box:hover { /* 电脑是平面,默认无法观察 Z 轴平移的效果 */ transform: translate3d(100px, 200px, 300px); /* 3d 小括号里面必须逗号隔开三个数 */ /* transform: translate3d(100px, 200px); */ /* transform: translateX(100px); transform: translateY(-100%); transform: translateZ(300px); */ } </style> </head> <body> <div class="box"></div> </body> </html>
视距 perspective
作用:指定了观察者与 Z=0 平面的距离,为元素添加透视效果
透视效果:近大远小、近实远虚
属性:(添加给父级,取值范围 800-1200)
perspective: 视距
示例:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> /* 视距属性必须直接添加给 直接父级 */ .father { perspective: 1000px; /* perspective: 10000px; perspective: 100px; */ } .son { width: 200px; height: 200px; margin: 100px auto; background-color: pink; transition: all 0.5s; } .son:hover { transform: translateZ(-300px); transform: translateZ(300px); } </style> </head> <body> <div class="father"> <div class="son"></div> </div> </body> </html>
旋转
- transform: rotateZ(值);
示例:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> .box { width: 300px; margin: 100px auto; } img { width: 300px; transition: all 2s; } .box img:hover { transform: rotateZ(360deg); } </style> </head> <body> <div class="box"> <img src="./images/hero.jpeg" alt=""> </div> </body> </html>
- transform: rotateX(值);
示例:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> .box { width: 300px; margin: 100px auto; } img { width: 300px; transition: all 2s; } .box { /* 透视效果:近大远小、近实远虚 */ perspective: 1000px; } .box img:hover { transform: rotateX(60deg); transform: rotateX(-60deg); } </style> </head> <body> <div class="box"> <img src="./images/hero.jpeg" alt=""> </div> </body> </html>
- transform: rotateY(值);
示例:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> .box { width: 300px; margin: 100px auto; } img { width: 300px; transition: all 2s; } .box { /* 透视效果:近大远小、近实远虚 */ perspective: 1000px; } .box img:hover { transform: rotateY(60deg); transform: rotateY(-60deg); } </style> </head> <body> <div class="box"> <img src="./images/hero.jpeg" alt=""> </div> </body> </html>
- 左手法则 - 根据旋转方向确定取值正负
左手握住旋转轴,拇指指向正值方向,其他四个手指弯曲方向为旋转正值方向
- 拓展
- rotate3d(x, y, z, 角度度数):用来设置自定义旋转轴的位置及旋转的角度
- x, y, z 取值为0-1之间的数字
立体呈现 - transform-style
作用:设置元素的子–元素是位于 3D 空间中还是平面==中
属性名:transform-style
属性值:
- flat:子级处于平面中
- preserve-3d:子级处于 3D 空间
- 呈现立体图形的步骤
- 父元素添加transform-style: preserve-3d;
- 子级定位
- 调整子盒子的位置(位移或旋转)
示例:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> .cube { position: relative; width: 200px; height: 200px; margin: 100px auto; /* background-color: pink; */ transition: all 2s; transform-style: preserve-3d; /* transform: rotateY(89deg); */ } .cube div { position: absolute; left: 0; top: 0; width: 200px; height: 200px; } .front { background-color: orange; transform: translateZ(100px); } .back { background-color: green; transform: translateZ(-100px); } .cube:hover { transform: rotateY(90deg); } </style> </head> <body> <div class="cube"> <div class="front">前面</div> <div class="back">后面</div> </div> </body> </html>
缩放
属性
transform: scale3d(x, y, z) transform: scaleX(); transform: scaleY(); transform: scaleZ();
2、动画 - animation
体验
- 过渡:实现两个状态间的拜年话过程
- 动画:实现多个状态间的变化过程,动画过程可控(重复播放、最终画面、是否暂停)
示例:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> div { width: 100px; height: 100px; background-color: pink; animation: change 1s infinite alternate; } @keyframes change { 0% { transform: translate(0); } 100% { transform: translate(600px); } } </style> </head> <body> <div></div> </body> </html>
实现步骤
定义动画
@keyframes 动画名称 { from {} to {} } @keyframes 动画名称 { 0% {} 10% {} ...... 100% {} }
使用动画
animation: 动画名称 动画花费时长;
示例:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> .box { width: 200px; height: 100px; background-color: pink; animation: change 1s; } /* 动画一:宽度从200变化到800 */ /* @keyframes change { from { width: 200px; } to { width: 800px; } } */ /* 动画二:从 200*100 变化到 300*300 变化到 800*500 */ /* 百分比:表示的意思是动画时长的百分比 */ @keyframes change { 0% { width: 200px; height: 100px; } 20% { width: 300px; height: 300px; } 100% { width: 800px; height: 500px; } } </style> </head> <body> <div class="box"></div> </body> </html>
属性
animation: 动画名称 动画时长 速度曲线 延迟时间 重复次数 动画方向 执行完毕时状态;
提示:
- 动画名称和动画时长必须赋值
- 取值不分先后顺序
- 如果有两个时间值,第一个时间表示动画时长,第二个时间表示延迟时间
示例:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> .box { width: 200px; height: 100px; background-color: pink; /* linear:匀速 */ animation: change 1s linear; /* steps:分布动画,工作中,配合精灵图实现精灵动画 */ animation: change 1s steps(3); /* 如果有两个时间值,第一个时间表示动画时长,第二个时间表示延迟时间 */ animation: change 1s 2s; /* 重复次数:infinite:无限循环 */ animation: change 1s 3; animation: change 1s infinite; /* alternate:反向 */ animation: change 1s infinite alternate; /* 动画执行完毕时的状态:forwards:结束状态;backwards:开始状态 */ animation: change 1s forwards; animation: change 1s backwards; } /* 宽度从 200 变化到 800 */ @keyframes change { from { width: 200px; } to { width: 800px; } } </style> </head> <body> <div class="box"></div> </body> </html>
属性 作用 取值 animation-name 动画名称 animation-duration 动画时长 animation-delay 延迟时间 animation-fill-mode 动画执行完毕时的状态 forwards:最后一帧
backwards:第一帧状态animation-timing-function 速度曲线 steps(数字):逐帧动画 animation-iteration-count 重复次数 infinite为无限循环 animation-direction 动画执行方向 alternate为反向 animation-play-state 暂停动画 paused为暂停,通常配合==:hover==使用
示例:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> .box { width: 200px; height: 100px; background-color: pink; /* 动画名称 */ animation-name: change; /* 动画时长 */ animation-duration: 1s; /* 播放次数 */ animation-iteration-count: infinite; } .box:hover { /* 暂停动画 */ animation-play-state: paused; } /* 宽度从 200 变化到 800 */ @keyframes change { 0% { width: 200px; } 100% { width: 800px; } } </style> </head> <body> <div class="box"></div> </body> </html>
逐帧动画
属性 作用 取值 animation-timing-function 速度曲线 steps(数字):逐帧动画 核心原理:
- steps() 逐帧动画
- CSS 精灵图
精灵动画制作步骤:
- 准备显示区域
盒子尺寸与一张精灵小图尺寸相同- 定义动画
移动背景图(移动距离 = 精灵图宽度)- 使用动画
steps(N),N 与精灵小图个数相同
示例:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> div { width: 140px; height: 140px; border: 1px solid #000; background-image: url(./images/bg.png); animation: run 1s steps(12) infinite; } @keyframes run { from { background-position: 0 0; } to { background-position: -1680px 0; } } </style> </head> <body> <div></div> </body> </html>
多组动画
animation: 动画1, 动画2, 动画N ; animation: run 1s steps(12) infinite, move 3s linear forwards ;
示例:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> div { width: 140px; height: 140px; background-image: url(./images/bg.png); animation: run 1s steps(12) infinite, move 3s linear forwards ; } /* 当动画的开始状态样式跟盒子默认样式相同时,可以省略动画开始状态的代码 */ @keyframes run { /* from { background-position: 0 0; } */ to { background-position: -1680px 0; } } @keyframes move { /* 0% { transform: translate(0); } */ 100% { transform: translate(800px); } } </style> </head> <body> <div></div> </body> </html>