资讯专栏INFORMATION COLUMN

前端每日实战:133# 视频演示如何用 CSS 和 GSAP 创作有多个关键帧的连续动画

keke / 2706人阅读

摘要:效果预览按下右侧的点击预览按钮可以在当前页面预览,点击链接可以全屏预览。可交互视频此视频是可以交互的,你可以随时暂停视频,编辑视频中的代码。隐藏容器外的内容,并删掉辅助线最后,装饰一下页面的角落大功告成

效果预览

按下右侧的“点击预览”按钮可以在当前页面预览,点击链接可以全屏预览。

https://codepen.io/comehope/pen/eLMKJG

可交互视频

此视频是可以交互的,你可以随时暂停视频,编辑视频中的代码。

请用 chrome, safari, edge 打开观看。

https://scrimba.com/p/pEgDAM/cdDRmH9

源代码下载

每日前端实战系列的全部源代码请从 github 下载:

https://github.com/comehope/front-end-daily-challenges

代码解读

定义 dom,容器中包含 10 个 div 子元素,每个 div 中包含 1 个 span 元素:

</>复制代码

居中显示:

</>复制代码

  1. body {
  2. margin: 0;
  3. height: 100vh;
  4. display: flex;
  5. align-items: center;
  6. justify-content: center;
  7. background-color: lightyellow;
  8. }

定义容器的尺寸和样式:

</>复制代码

  1. .container {
  2. width: 400px;
  3. height: 400px;
  4. background: linear-gradient(45deg, tomato, gold);
  5. border-radius: 3%;
  6. box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
  7. }

画出容器里的 1 个元素,它有一个外壳 div,里面是一个白色的小方块 span

</>复制代码

  1. .container {
  2. position: relative;
  3. }
  4. .container div {
  5. position: absolute;
  6. width: inherit;
  7. height: inherit;
  8. display: flex;
  9. align-items: center;
  10. justify-content: center;
  11. }
  12. .container div span {
  13. position: absolute;
  14. width: 40px;
  15. height: 40px;
  16. background-color: white;
  17. }

为容器中的元素定义下标变量,并让元素的外壳依次旋转,围合成一个圆形,其中 outline 是辅助线:

</>复制代码

  1. .container div {
  2. outline: 1px dashed black;
  3. transform: rotate(calc((var(--n) - 1) * 36deg));
  4. }
  5. .container div:nth-child(1) { --n: 1; }
  6. .container div:nth-child(2) { --n: 2; }
  7. .container div:nth-child(3) { --n: 3; }
  8. .container div:nth-child(4) { --n: 4; }
  9. .container div:nth-child(5) { --n: 5; }
  10. .container div:nth-child(6) { --n: 6; }
  11. .container div:nth-child(7) { --n: 7; }
  12. .container div:nth-child(8) { --n: 8; }
  13. .container div:nth-child(9) { --n: 9; }
  14. .container div:nth-child(10) { --n: 10; }

至此,子元素绘制完成,接下来开始写动画脚本。
引入 GSAP 库:

</>复制代码

定义一个变量,代表子元素选择器:

</>复制代码

  1. let elements = ".container div span";

声明一个时间线对象:

</>复制代码

  1. let animation = new TimelineMax();

先设定入场方式为由小(第1帧)变大(第2帧),其中并没有第 2 帧的代码,它是隐含在语义中的:

</>复制代码

  1. animation.from(elements, 1, {scale: 0});

让子元素变成竖长条,向四周散开(第3帧):

</>复制代码

  1. animation.from(elements, 1, {scale: 0})
  2. .to(elements, 1, {y: "-100px", scaleX: 0.25});

让竖长条旋转着变成小方块(第4帧):

</>复制代码

  1. animation.from(elements, 1, {scale: 0})
  2. .to(elements, 1, {y: "-100px", scaleX: 0.25})
  3. .to(elements, 1, {scaleY: 0.25, rotation: 180});

让小方块变成横长条,围成一个圆形(第5帧):

</>复制代码

  1. animation.from(elements, 1, {scale: 0})
  2. .to(elements, 1, {y: "-100px", scaleX: 0.25})
  3. .to(elements, 1, {scaleY: 0.25, rotation: 180})
  4. .to(elements, 1, {scaleX: 1});

注意,因 scrimba 在录制过多帧时会崩溃,所以第 6 帧至第 11 帧没有在视频中体现。
让圆形向内收敛,同时线条变细(第6帧):

</>复制代码

  1. animation.from(elements, 1, {scale: 0})
  2. .to(elements, 1, {y: "-100px", scaleX: 0.25})
  3. .to(elements, 1, {scaleY: 0.25, rotation: 180})
  4. .to(elements, 1, {scaleX: 1})
  5. .to(elements, 1, {y: "-60px", scaleY: 0.1});

让线条向左摆动(第7帧):

</>复制代码

  1. animation.from(elements, 1, {scale: 0})
  2. .to(elements, 1, {y: "-100px", scaleX: 0.25})
  3. .to(elements, 1, {scaleY: 0.25, rotation: 180})
  4. .to(elements, 1, {scaleX: 1})
  5. .to(elements, 1, {y: "-60px", scaleY: 0.1})
  6. .to(elements, 1, {x: "-30px"});

再让线条向右摆动(第8帧):

</>复制代码

  1. animation.from(elements, 1, {scale: 0})
  2. .to(elements, 1, {y: "-100px", scaleX: 0.25})
  3. .to(elements, 1, {scaleY: 0.25, rotation: 180})
  4. .to(elements, 1, {scaleX: 1})
  5. .to(elements, 1, {y: "-60px", scaleY: 0.1})
  6. .to(elements, 1, {x: "-30px"})
  7. .to(elements, 1, {x: "30px"});

再把横线变为竖线,造型与第 3 帧相似,只是线更细,更向内收敛(第9帧):

</>复制代码

  1. animation.from(elements, 1, {scale: 0})
  2. .to(elements, 1, {y: "-100px", scaleX: 0.25})
  3. .to(elements, 1, {scaleY: 0.25, rotation: 180})
  4. .to(elements, 1, {scaleX: 1})
  5. .to(elements, 1, {y: "-60px", scaleY: 0.1})
  6. .to(elements, 1, {x: "-30px"})
  7. .to(elements, 1, {x: "30px"})
  8. .to(elements, 1, {x: "0", scaleX: 0.1, scaleY: 1});

再把竖线变为横线,造型与第 5 帧相似,但线短一些(第10帧):

</>复制代码

  1. animation.from(elements, 1, {scale: 0})
  2. .to(elements, 1, {y: "-100px", scaleX: 0.25})
  3. .to(elements, 1, {scaleY: 0.25, rotation: 180})
  4. .to(elements, 1, {scaleX: 1})
  5. .to(elements, 1, {y: "-60px", scaleY: 0.1})
  6. .to(elements, 1, {x: "-30px"})
  7. .to(elements, 1, {x: "30px"})
  8. .to(elements, 1, {x: "0", scaleX: 0.1, scaleY: 1})
  9. .to(elements, 1, {scaleX: 0.5, scaleY: 0.1})

横线稍向外扩散,变为圆点(第11帧):

</>复制代码

  1. animation.from(elements, 1, {scale: 0})
  2. .to(elements, 1, {y: "-100px", scaleX: 0.25})
  3. .to(elements, 1, {scaleY: 0.25, rotation: 180})
  4. .to(elements, 1, {scaleX: 1})
  5. .to(elements, 1, {y: "-60px", scaleY: 0.1})
  6. .to(elements, 1, {x: "-30px"})
  7. .to(elements, 1, {x: "30px"})
  8. .to(elements, 1, {x: "0", scaleX: 0.1, scaleY: 1})
  9. .to(elements, 1, {scaleX: 0.5, scaleY: 0.1})
  10. .to(elements, 1, {y: "-80px", scaleY: 0.5, borderRadius: "50%"});

让圆点变形为竖线,并向内收缩,这个变化的距离长,所以动画时间也要长一些(第12帧):

</>复制代码

  1. animation.from(elements, 1, {scale: 0})
  2. .to(elements, 1, {y: "-100px", scaleX: 0.25})
  3. .to(elements, 1, {scaleY: 0.25, rotation: 180})
  4. .to(elements, 1, {scaleX: 1})
  5. .to(elements, 1, {y: "-60px", scaleY: 0.1})
  6. .to(elements, 1, {x: "-30px"})
  7. .to(elements, 1, {x: "30px"})
  8. .to(elements, 1, {x: "0", scaleX: 0.1, scaleY: 1})
  9. .to(elements, 1, {scaleX: 0.5, scaleY: 0.1})
  10. .to(elements, 1, {y: "-80px", scaleY: 0.5, borderRadius: "50%"})
  11. .to(elements, 1, {y: "-10px", scaleX: 0.1, scaleY: 0.5, borderRadius: "0%", rotation: 0});

让竖线从中心向外快速扩散,扩散前稍停片刻,好像线条都被发射出一样(第13帧):

</>复制代码

  1. animation.from(elements, 1, {scale: 0})
  2. .to(elements, 1, {y: "-100px", scaleX: 0.25})
  3. .to(elements, 1, {scaleY: 0.25, rotation: 180})
  4. .to(elements, 1, {scaleX: 1})
  5. .to(elements, 1, {y: "-60px", scaleY: 0.1})
  6. .to(elements, 1, {x: "-30px"})
  7. .to(elements, 1, {x: "30px"})
  8. .to(elements, 1, {x: "0", scaleX: 0.1, scaleY: 1})
  9. .to(elements, 1, {scaleX: 0.5, scaleY: 0.1})
  10. .to(elements, 1, {y: "-80px", scaleY: 0.5, borderRadius: "50%"})
  11. .to(elements, 1, {y: "-10px", scaleX: 0.1, scaleY: 0.5, borderRadius: "0%", rotation: 0})
  12. .to(elements, 1, {y: "-300px", delay: 0.5});

用时间尺度缩放函数让动画播放速度加快一倍:

</>复制代码

  1. animation.from(elements, 1, {scale: 0})
  2. .to(elements, 1, {y: "-100px", scaleX: 0.25})
  3. .to(elements, 1, {scaleY: 0.25, rotation: 180})
  4. .to(elements, 1, {scaleX: 1})
  5. .to(elements, 1, {y: "-60px", scaleY: 0.1})
  6. .to(elements, 1, {x: "-30px"})
  7. .to(elements, 1, {x: "30px"})
  8. .to(elements, 1, {x: "0", scaleX: 0.1, scaleY: 1})
  9. .to(elements, 1, {scaleX: 0.5, scaleY: 0.1})
  10. .to(elements, 1, {y: "-80px", scaleY: 0.5, borderRadius: "50%"})
  11. .to(elements, 1, {y: "-10px", scaleX: 0.1, scaleY: 0.5, borderRadius: "0%", rotation: 0})
  12. .to(elements, 1, {y: "-300px", delay: 0.5})
  13. .timeScale(2);

修改声明时间线的代码,使动画重复播放:

</>复制代码

  1. let animation = new TimelineMax({repeat: -1, repeatDelay: 1});

至此,动画完成。
隐藏容器外的内容,并删掉辅助线;

</>复制代码

  1. .container {
  2. overflow: hidden;
  3. }
  4. .container div {
  5. /* outline: 1px dashed black; */
  6. }

最后,装饰一下页面的角落:

</>复制代码

  1. body {
  2. overflow: hidden;
  3. }
  4. body::before,
  5. body::after {
  6. content: "";
  7. position: absolute;
  8. width: 60vmin;
  9. height: 60vmin;
  10. border-radius: 50%;
  11. background: radial-gradient(
  12. transparent 25%,
  13. gold 25%, gold 50%,
  14. tomato 50%
  15. );
  16. }
  17. body::before {
  18. left: -30vmin;
  19. bottom: -30vmin;
  20. }
  21. body::after {
  22. right: -30vmin;
  23. top: -30vmin;
  24. }

大功告成!

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/97681.html

相关文章

  • 前端每日实战 2018 年 9 月份项目汇总(共 26 个项目)

    摘要:过往项目年月份项目汇总共个项目年月份项目汇总共个项目年月份项目汇总共个项目年月份项目汇总共个项目年月份项目汇总共个项目年月份发布的项目前端每日实战专栏每天分解一个前端项目,用视频记录编码过程,再配合详细的代码解读,是学习前端开发的活的参考书 过往项目 2018 年 8 月份项目汇总(共 29 个项目) 2018 年 7 月份项目汇总(共 29 个项目) 2018 年 6 月份项目汇总(...

    lavnFan 评论0 收藏0
  • 前端每日实战 2018 年 9 月份项目汇总(共 26 个项目)

    摘要:过往项目年月份项目汇总共个项目年月份项目汇总共个项目年月份项目汇总共个项目年月份项目汇总共个项目年月份项目汇总共个项目年月份发布的项目前端每日实战专栏每天分解一个前端项目,用视频记录编码过程,再配合详细的代码解读,是学习前端开发的活的参考书 过往项目 2018 年 8 月份项目汇总(共 29 个项目) 2018 年 7 月份项目汇总(共 29 个项目) 2018 年 6 月份项目汇总(...

    whatsns 评论0 收藏0
  • 前端每日实战133# 视频演示何用 CSS GSAP 创作多个关键帧的连续动画

    摘要:效果预览按下右侧的点击预览按钮可以在当前页面预览,点击链接可以全屏预览。可交互视频此视频是可以交互的,你可以随时暂停视频,编辑视频中的代码。隐藏容器外的内容,并删掉辅助线最后,装饰一下页面的角落大功告成 showImg(https://segmentfault.com/img/bVbgOQt?w=400&h=302); 效果预览 按下右侧的点击预览按钮可以在当前页面预览,点击链接可以全...

    forsigner 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<