如何使用CSS和GSAP实现有多个关键帧的连续动画(附源码)
本篇文章给大家带来的内容是关于如何使用CSS和GSAP实现有多个关键帧的连续动画(附源码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
效果预览

源代码下载
https://github.com/comehope/front-end-daily-challenges
代码解读
定义 dom,容器中包含 10 个 p
子元素,每个 p
中包含 1 个 span
元素:
<figure class="container"> <div><span></span></div> <div><span></span></div> <div><span></span></div> <div><span></span></div> <div><span></span></div> <div><span></span></div> <div><span></span></div> <div><span></span></div> <div><span></span></div> <div><span></span></div> </figure>
居中显示:
body { margin: 0; height: 100vh; display: flex; align-items: center; justify-content: center; background-color: lightyellow; }
定义容器的尺寸和样式:
.container { width: 400px; height: 400px; background: linear-gradient(45deg, tomato, gold); border-radius: 3%; box-shadow: 0 0 10px rgba(0, 0, 0, 0.3); }
画出容器里的 1 个元素,它有一个外壳 p
,里面是一个白色的小方块 span
:
.container { position: relative; } .container p { position: absolute; width: inherit; height: inherit; display: flex; align-items: center; justify-content: center; } .container p span { position: absolute; width: 40px; height: 40px; background-color: white; }
为容器中的元素定义下标变量,并让元素的外壳依次旋转,围合成一个圆形,其中 outline
是辅助线:
.container p { outline: 1px dashed black; transform: rotate(calc((var(--n) - 1) * 36deg)); } .container p:nth-child(1) { --n: 1; } .container p:nth-child(2) { --n: 2; } .container p:nth-child(3) { --n: 3; } .container p:nth-child(4) { --n: 4; } .container p:nth-child(5) { --n: 5; } .container p:nth-child(6) { --n: 6; } .container p:nth-child(7) { --n: 7; } .container p:nth-child(8) { --n: 8; } .container p:nth-child(9) { --n: 9; } .container p:nth-child(10) { --n: 10; }
至此,子元素绘制完成,接下来开始写动画脚本。
引入 GSAP 库:
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/2.0.2/TweenMax.min.js"></script>
定义一个变量,代表子元素选择器:
let elements = '.container p span';
声明一个时间线对象:
let animation = new TimelineMax();
先设定入场方式为由小(第1帧)变大(第2帧),其中并没有第 2 帧的代码,它是隐含在语义中的:
animation.from(elements, 1, {scale: 0});
让子元素变成竖长条,向四周散开(第3帧):
animation.from(elements, 1, {scale: 0}) .to(elements, 1, {y: '-100px', scaleX: 0.25});
让竖长条旋转着变成小方块(第4帧):
animation.from(elements, 1, {scale: 0}) .to(elements, 1, {y: '-100px', scaleX: 0.25}) .to(elements, 1, {scaleY: 0.25, rotation: 180});
让小方块变成横长条,围成一个圆形(第5帧):
animation.from(elements, 1, {scale: 0}) .to(elements, 1, {y: '-100px', scaleX: 0.25}) .to(elements, 1, {scaleY: 0.25, rotation: 180}) .to(elements, 1, {scaleX: 1});
注意,因 scrimba 在录制过多帧时会崩溃,所以第 6 帧至第 11 帧没有在视频中体现。
让圆形向内收敛,同时线条变细(第6帧):
animation.from(elements, 1, {scale: 0}) .to(elements, 1, {y: '-100px', scaleX: 0.25}) .to(elements, 1, {scaleY: 0.25, rotation: 180}) .to(elements, 1, {scaleX: 1}) .to(elements, 1, {y: '-60px', scaleY: 0.1});
让线条向左摆动(第7帧):
animation.from(elements, 1, {scale: 0}) .to(elements, 1, {y: '-100px', scaleX: 0.25}) .to(elements, 1, {scaleY: 0.25, rotation: 180}) .to(elements, 1, {scaleX: 1}) .to(elements, 1, {y: '-60px', scaleY: 0.1}) .to(elements, 1, {x: '-30px'});
再让线条向右摆动(第8帧):
animation.from(elements, 1, {scale: 0}) .to(elements, 1, {y: '-100px', scaleX: 0.25}) .to(elements, 1, {scaleY: 0.25, rotation: 180}) .to(elements, 1, {scaleX: 1}) .to(elements, 1, {y: '-60px', scaleY: 0.1}) .to(elements, 1, {x: '-30px'}) .to(elements, 1, {x: '30px'});
再把横线变为竖线,造型与第 3 帧相似,只是线更细,更向内收敛(第9帧):
animation.from(elements, 1, {scale: 0}) .to(elements, 1, {y: '-100px', scaleX: 0.25}) .to(elements, 1, {scaleY: 0.25, rotation: 180}) .to(elements, 1, {scaleX: 1}) .to(elements, 1, {y: '-60px', scaleY: 0.1}) .to(elements, 1, {x: '-30px'}) .to(elements, 1, {x: '30px'}) .to(elements, 1, {x: '0', scaleX: 0.1, scaleY: 1});
再把竖线变为横线,造型与第 5 帧相似,但线短一些(第10帧):
animation.from(elements, 1, {scale: 0}) .to(elements, 1, {y: '-100px', scaleX: 0.25}) .to(elements, 1, {scaleY: 0.25, rotation: 180}) .to(elements, 1, {scaleX: 1}) .to(elements, 1, {y: '-60px', scaleY: 0.1}) .to(elements, 1, {x: '-30px'}) .to(elements, 1, {x: '30px'}) .to(elements, 1, {x: '0', scaleX: 0.1, scaleY: 1}) .to(elements, 1, {scaleX: 0.5, scaleY: 0.1})
横线稍向外扩散,变为圆点(第11帧):
animation.from(elements, 1, {scale: 0}) .to(elements, 1, {y: '-100px', scaleX: 0.25}) .to(elements, 1, {scaleY: 0.25, rotation: 180}) .to(elements, 1, {scaleX: 1}) .to(elements, 1, {y: '-60px', scaleY: 0.1}) .to(elements, 1, {x: '-30px'}) .to(elements, 1, {x: '30px'}) .to(elements, 1, {x: '0', scaleX: 0.1, scaleY: 1}) .to(elements, 1, {scaleX: 0.5, scaleY: 0.1}) .to(elements, 1, {y: '-80px', scaleY: 0.5, borderRadius: '50%'});
让圆点变形为竖线,并向内收缩,这个变化的距离长,所以动画时间也要长一些(第12帧):
animation.from(elements, 1, {scale: 0}) .to(elements, 1, {y: '-100px', scaleX: 0.25}) .to(elements, 1, {scaleY: 0.25, rotation: 180}) .to(elements, 1, {scaleX: 1}) .to(elements, 1, {y: '-60px', scaleY: 0.1}) .to(elements, 1, {x: '-30px'}) .to(elements, 1, {x: '30px'}) .to(elements, 1, {x: '0', scaleX: 0.1, scaleY: 1}) .to(elements, 1, {scaleX: 0.5, scaleY: 0.1}) .to(elements, 1, {y: '-80px', scaleY: 0.5, borderRadius: '50%'}) .to(elements, 1, {y: '-10px', scaleX: 0.1, scaleY: 0.5, borderRadius: '0%', rotation: 0});
让竖线从中心向外快速扩散,扩散前稍停片刻,好像线条都被发射出一样(第13帧):
animation.from(elements, 1, {scale: 0}) .to(elements, 1, {y: '-100px', scaleX: 0.25}) .to(elements, 1, {scaleY: 0.25, rotation: 180}) .to(elements, 1, {scaleX: 1}) .to(elements, 1, {y: '-60px', scaleY: 0.1}) .to(elements, 1, {x: '-30px'}) .to(elements, 1, {x: '30px'}) .to(elements, 1, {x: '0', scaleX: 0.1, scaleY: 1}) .to(elements, 1, {scaleX: 0.5, scaleY: 0.1}) .to(elements, 1, {y: '-80px', scaleY: 0.5, borderRadius: '50%'}) .to(elements, 1, {y: '-10px', scaleX: 0.1, scaleY: 0.5, borderRadius: '0%', rotation: 0}) .to(elements, 1, {y: '-300px', delay: 0.5});
用时间尺度缩放函数让动画播放速度加快一倍:
animation.from(elements, 1, {scale: 0}) .to(elements, 1, {y: '-100px', scaleX: 0.25}) .to(elements, 1, {scaleY: 0.25, rotation: 180}) .to(elements, 1, {scaleX: 1}) .to(elements, 1, {y: '-60px', scaleY: 0.1}) .to(elements, 1, {x: '-30px'}) .to(elements, 1, {x: '30px'}) .to(elements, 1, {x: '0', scaleX: 0.1, scaleY: 1}) .to(elements, 1, {scaleX: 0.5, scaleY: 0.1}) .to(elements, 1, {y: '-80px', scaleY: 0.5, borderRadius: '50%'}) .to(elements, 1, {y: '-10px', scaleX: 0.1, scaleY: 0.5, borderRadius: '0%', rotation: 0}) .to(elements, 1, {y: '-300px', delay: 0.5}) .timeScale(2);
修改声明时间线的代码,使动画重复播放:
let animation = new TimelineMax({repeat: -1, repeatDelay: 1});
至此,动画完成。
隐藏容器外的内容,并删掉辅助线;
.container { overflow: hidden; } .container p { /* outline: 1px dashed black; */ }
最后,装饰一下页面的角落:
body { overflow: hidden; } body::before, body::after { content: ''; position: absolute; width: 60vmin; height: 60vmin; border-radius: 50%; background: radial-gradient( transparent 25%, gold 25%, gold 50%, tomato 50% ); } body::before { left: -30vmin; bottom: -30vmin; } body::after { right: -30vmin; top: -30vmin; }
大功告成!
相关推荐:
以上是如何使用CSS和GSAP实现有多个关键帧的连续动画(附源码)的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

如何使用 Bootstrap 按钮?引入 Bootstrap CSS创建按钮元素并添加 Bootstrap 按钮类添加按钮文本

要调整 Bootstrap 中元素大小,可以使用尺寸类,具体包括:调整宽度:.col-、.w-、.mw-调整高度:.h-、.min-h-、.max-h-

答案:可以使用 Bootstrap 的日期选择器组件在页面中查看日期。步骤:引入 Bootstrap 框架。在 HTML 中创建日期选择器输入框。Bootstrap 将自动为选择器添加样式。使用 JavaScript 获取选定的日期。

创建 Bootstrap 分割线有两种方法:使用 标签,可创建水平分割线。使用 CSS border 属性,可创建自定义样式的分割线。

要设置 Bootstrap 框架,需要按照以下步骤:1. 通过 CDN 引用 Bootstrap 文件;2. 下载文件并将其托管在自己的服务器上;3. 在 HTML 中包含 Bootstrap 文件;4. 根据需要编译 Sass/Less;5. 导入定制文件(可选)。设置完成后,即可使用 Bootstrap 的网格系统、组件和样式创建响应式网站和应用程序。

在 Bootstrap 中插入图片有以下几种方法:直接插入图片,使用 HTML 的 img 标签。使用 Bootstrap 图像组件,可以提供响应式图片和更多样式。设置图片大小,使用 img-fluid 类可以使图片自适应。设置边框,使用 img-bordered 类。设置圆角,使用 img-rounded 类。设置阴影,使用 shadow 类。调整图片大小和位置,使用 CSS 样式。使用背景图片,使用 background-image CSS 属性。

在 Bootstrap 中验证日期,需遵循以下步骤:引入必需的脚本和样式;初始化日期选择器组件;设置 data-bv-date 属性以启用验证;配置验证规则(如日期格式、错误消息等);集成 Bootstrap 验证框架,并在表单提交时自动验证日期输入。

在 Vue.js 中使用 Bootstrap 分为五个步骤:安装 Bootstrap。在 main.js 中导入 Bootstrap。直接在模板中使用 Bootstrap 组件。可选:自定义样式。可选:使用插件。
