前言
在网页制作课程大作业时,在网上抄了一个看起来很帅的3D轮播图,不过并不了解怎么实现,非常好奇,于是又重新查资料,整理了一下关于CSS实现动画的一些基本语法,希望大家也能通过自己的好奇心,自主的学习更多知识。
大纲预览
首先介绍3D变换的平移,选择,缩放,原点与过度
接着了解动画的实现
最后扩展分享一个3D轮播图的案例
3D变换
注:transform不会改变元素在文档流中的位置(即它不会改变元素的 top
、right
、bottom
、left
或 z-index
属性),但它会改变元素在页面上的视觉呈现。
基本语法
1.位移:translate3d translateX translateY translateZ
格式:transform:translate3d(x,y,z)/translateX(x)……
参数单位:px
x,y,z:为移动量,正方向为:右,上,前(沿Z轴向屏幕外移动)
2.旋转:rotate3d rotateX rotateY rotateZ
格式:transform:rotate(a,b,c,angle)/rotateX(n)……
单位:deg
rotate3d中:a,b,c确定旋转点,angle为旋转度数;
rotateX(n)为绕x轴旋转n度,同理rotatY(n)为绕y轴旋转n度;正数为顺时针
3.缩放:scale3d scaleX scaleY scaleZ
格式:transform:scale3d(x,y,z)/scaleX(x)
单位:数字(缩放倍数:大于1放大,小于1缩小)
4.变换原点
原点就是发生变换的轴点,可以通过transform-origin属性改变原点的位置;
设置偏移值都是重左上角开始的;默认为:transform-origin:center center;
原点设置关键字:top,bottom,center,left。right;也可以使用px,em,百分比等设置(不常用)
5.父元素样式
3D变换效果需要通过设置父元素样式才能保留3d的变化效果
保留3d变换:transform-style:preserve-3d
(此属性指定了嵌套元素在3D空间中的呈现方式。)
设置视角深度:perspective:1000px
(此属性定义了观察者与z=0平面的距离,即透视距离。)
6.过度
概念:元素从一种属性的取值逐渐变化为另一种同样属性的取transition:值(由于是慢慢改变,所以会有过渡的效果)
最简单的设置:transition:1s
(过度花费1s)
过度的属性名称transition-property
:属性名(none:补变换)
过度演示执行时间transition-delay:ns
(n秒后发生过度)
案例解析
可以通过案例来观察变换的情况
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>案例解析</title>
<style> body{ transform-style:preserve-3d ; perspective: 1000px; } .box{ width: 350px; background-color: rgb(0, 242, 255); padding: 50px 10px 50px 10px; transform-style:preserve-3d ; perspective: 1000px;
;
} .a{ width: 50px; height: 50px; background-color: red; margin-bottom: 10px; font-size: 16px; color: black; text-align: center; line-height: 50px; transition: 1s; }
.a1:active{ transform: translate(100px); }
.a2:active{ transform: translateZ(100px); }
.a3:active{ transform: rotateX(60deg); }
.a4:active{ transform: scale3d(4,2,0); } .a5:active{ transform: rotate(30deg); transform-origin:left bottom; }
</style>
</head> <body> <div class="box"> <div class="a a1">位移</div> <div class="a a2">位移</div> <div class="a a3">旋转</div> <div class="a a4">缩放</div> <div class="a a5">旋转</div> </div>
</body> </html>
|
动画的实现
定义:通过@keyframes
规则定义一系列的关键帧,每个关键帧描述了动画过程中的一个特定点(如0%、50%、100%等)的样式。然后,通过animation
属性将这些关键帧应用到元素上,并控制动画的播放方式。
规则:先创建再使用
创建动画 @keyframes规则
定义动画名字:@keyframes 动画名字
动画持续时间的百分比(不同百分比执行不同的事件):keyframes-scelector
- 0%~100%
- from(和0%一样)
- to(和100%一样)
使用动画名称 animation-name:名字
设置动画完成一个周期所有的时间:animation-duration:ns
(默认为0秒)
设置动画执行次数animation-iteration-count:n
(默认为0次,infinite为一直执行)
设置动画开始播放前的延时时间animation-delay:ns
(默认为1秒)
实例解析
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>案例解析</title>
<style> .box{ width: 350px; background-color: rgb(0, 242, 255); padding: 50px 10px 50px 10px; } .a{ width: 50px; height: 50px; background-color: red; margin-bottom: 10px; font-size: 16px; color: black; text-align: center; line-height: 50px; } @keyframes identifier1 { 0%{ transform: translate(0px); } 20%{ transform: translate(0px,200px); } 40%{ transform: translate(200px,200px); } 60%{ transform: translate(200px,100px); } 80%{ transform: translate(0,100px); } 100%{ transform: translate(0px); } } .a1{ animation-name: identifier1; animation-duration: 3.5s; animation-iteration-count:5; animation-delay: 2s; } </style>
</head> <body> <div class="box"> <div class="a a1">位移</div> </div> </body> </html>
|
扩展3D轮播图案例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
| <!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> body{ background-color: blue; } .旋转盒子{ display: flex; justify-content: center; align-items: center; margin: 0; padding: 0; border: 1px solid black; width: 1536px; height: 800px; } .a{ position: relative; perspective: 1600px; top: 0; display: flex; justify-content: center; align-items: center; } .b{ width: 400px; height: 250px; position: absolute; transform-style: preserve-3d; animation: a 8s infinite cubic-bezier(0.075, 0.82, 0.165, 1); } .b div{ position: absolute; background-size: cover; opacity: .9; width: 400px; height: 250px; transform: rotateY(calc(var(--i)*120deg)) translateZ(600px) ; } @keyframes a{ 0%{ transform: translateZ(-100px) rotateY(0); } 33%{ transform: translateZ(-100px) rotateY(-120deg); } 66%{ transform: translateZ(-100px) rotateY(-240deg); } 100%{ transform: translateZ(-100px) rotateY(-360deg); } }
}
}
} </style> </head> <body> <div class="旋转盒子"> <div class="a"> <div class="b"> <div class="c" style="--i:0"></div> <div class="d" style="--i:1"></div> <div class="e" style="--i:2"></div> </div> </div> </div> </body> </html>
|