效果预览

在线演示

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

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

可交互视频

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

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

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

源代码下载

本地下载

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

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

代码解读

定义 dom,容器中包含 5 个元素,代表 5 个台阶:

  1. <div class="loader">
  2. <span></span>
  3. <span></span>
  4. <span></span>
  5. <span></span>
  6. <span></span>
  7. </div>

居中显示:

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

定义容器尺寸:

  1. .loader {
  2. width: 7em;
  3. height: 5em;
  4. font-size: 40px;
  5. }

画出 5 个台阶:

  1. .loader {
  2. display: flex;
  3. justify-content: space-between;
  4. align-items: flex-end;
  5. }
  6. .loader span {
  7. width: 1em;
  8. height: 1em;
  9. background-color: white;
  10. }

用变量让 5 个台阶从低到高排序:

  1. .loader span {
  2. height: calc(var(--n) * 1em);
  3. }
  4. .loader span:nth-child(1) {
  5. --n: 1;
  6. }
  7. .loader span:nth-child(2) {
  8. --n: 2;
  9. }
  10. .loader span:nth-child(3) {
  11. --n: 3;
  12. }
  13. .loader span:nth-child(4) {
  14. --n: 4;
  15. }
  16. .loader span:nth-child(5) {
  17. --n: 5;
  18. }

为台阶增加转换排序方向的动画效果:

  1. .loader span {
  2. animation: sort 5s infinite;
  3. }
  4. @keyframes sort {
  5. 0%, 40%, 100% {
  6. height: calc(var(--n) * 1em);
  7. }
  8. 50%, 90% {
  9. height: calc(5em - (var(--n) - 1) * 1em);
  10. }
  11. }

下面做小球的动画,用了障眼法,使 2 个同色小球的交替运动看起来就像 1 个小球在做往复运动。

用伪元素画出 2 个小球:

  1. .loader::before,
  2. .loader::after {
  3. content: '';
  4. position: absolute;
  5. width: 1em;
  6. height: 1em;
  7. background-color: white;
  8. border-radius: 50%;
  9. bottom: 1em;
  10. }
  11. .loader::before {
  12. left: 0;
  13. }
  14. .loader::after {
  15. left: 6em;
  16. }

增加让小球向上运动的动画效果:

  1. .loader::before,
  2. .loader::after {
  3. animation: climbing 5s infinite;
  4. visibility: hidden;
  5. }
  6. .loader::after {
  7. animation-delay: 2.5s;
  8. }
  9. @keyframes climbing {
  10. 0% {
  11. bottom: 1em;
  12. visibility: visible;
  13. }
  14. 10% {
  15. bottom: 2em;
  16. }
  17. 20% {
  18. bottom: 3em;
  19. }
  20. 30% {
  21. bottom: 4em;
  22. }
  23. 40% {
  24. bottom: 5em;
  25. }
  26. 50% {
  27. bottom: 1em;
  28. }
  29. 50%, 100% {
  30. visibility: hidden;
  31. }
  32. }

在向上运动的同时向两侧运动,形成上台阶的动画效果:

  1. .loader::before {
  2. --direction: 1;
  3. }
  4. .loader::after {
  5. --direction: -1;
  6. }
  7. @keyframes climbing {
  8. 0% {
  9. bottom: 1em;
  10. left: calc(3em - 2 * 1.5em * var(--direction));
  11. visibility: visible;
  12. }
  13. 10% {
  14. bottom: 2em;
  15. left: calc(3em - 1 * 1.5em * var(--direction));
  16. }
  17. 20% {
  18. bottom: 3em;
  19. left: calc(3em - 0 * 1.5em * var(--direction));
  20. }
  21. 30% {
  22. bottom: 4em;
  23. left: calc(3em + 1 * 1.5em * var(--direction));
  24. }
  25. 40% {
  26. bottom: 5em;
  27. left: calc(3em + 2 * 1.5em * var(--direction));
  28. }
  29. 50% {
  30. bottom: 1em;
  31. left: calc(3em + 2 * 1.5em * var(--direction));
  32. }
  33. 50%, 100% {
  34. visibility: hidden;
  35. }
  36. }

最后,为上台阶的动作增加拟人效果:

  1. @keyframes climbing {
  2. 0% {
  3. bottom: 1em;
  4. left: calc(3em - 2 * 1.5em * var(--direction));
  5. visibility: visible;
  6. }
  7. 7% {
  8. bottom: calc(2em + 0.3em);
  9. }
  10. 10% {
  11. bottom: 2em;
  12. left: calc(3em - 1 * 1.5em * var(--direction));
  13. }
  14. 17% {
  15. bottom: calc(3em + 0.3em);
  16. }
  17. 20% {
  18. bottom: 3em;
  19. left: calc(3em - 0 * 1.5em * var(--direction));
  20. }
  21. 27% {
  22. bottom: calc(4em + 0.3em);
  23. }
  24. 30% {
  25. bottom: 4em;
  26. left: calc(3em + 1 * 1.5em * var(--direction));
  27. }
  28. 37% {
  29. bottom: calc(5em + 0.3em);
  30. }
  31. 40% {
  32. bottom: 5em;
  33. left: calc(3em + 2 * 1.5em * var(--direction));
  34. }
  35. 50% {
  36. bottom: 1em;
  37. left: calc(3em + 2 * 1.5em * var(--direction));
  38. }
  39. 50%, 100% {
  40. visibility: hidden;
  41. }
  42. }

大功告成!

原文地址:https://segmentfault.com/a/1190000015686159

如何用纯 CSS 创作一个小球上台阶的动画的更多相关文章

  1. 如何用纯 CSS 创作一个小球反弹的动画

    效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/OwWROO 可交互视频 ...

  2. 前端每日实战:85# 视频演示如何用纯 CSS 创作一个小球反弹的动画

    效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/OwWROO 可交互视频 此视频是可 ...

  3. 前端每日实战:52# 视频演示如何用纯 CSS 创作一个小球绕着圆环盘旋的动画

    效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/gKxyWo 可交互视频 此视频是可 ...

  4. 如何用纯 CSS 创作一个记事本翻页动画

    效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/qKOPGw 可交互视频教 ...

  5. 前端每日实战:46# 视频演示如何用纯 CSS 创作一个在容器中反弹的小球

    效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/jKVbyE 可交互视频教程 此视频 ...

  6. 52.纯 CSS 创作一个小球绕着圆环盘旋的动画

    原文地址:https://segmentfault.com/a/1190000015295466 感想:重点在小球绕环转动. HTML code: <div class="contai ...

  7. 如何用纯 CSS 创作一个菜单反色填充特效

    效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览.https://codepen.io/comehope/pen/qYMoPo 可交互视频教程 ...

  8. 前端每日实战:23# 视频演示如何用纯 CSS 创作一个菜单反色填充特效

    效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览.https://codepen.io/comehope/pen/qYMoPo 可交互视频教程 此视频是 ...

  9. 纯 CSS 创作一个小球绕着圆环盘旋的动画

    效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/gKxyWo 可交互视频 ...

随机推荐

  1. Linux各文件颜色的含义

    Linux系统中文件有多种颜色,不同颜色文件代表不同类型的文件,具体如下: 蓝色:目录 绿色:可执行文件 红色:压缩文件 浅蓝色:链接文件 白色:普通文件 黄色:设备文件

  2. 第三篇:POSIX标准中的 “ 限制 ”

    前言 在POSIX标准中,定义了许多限制.这些限制大约分为五类,不同类型的限制获取的方式不一样. 限制值分类 1. 不变的最小值 这类型的限制值是静态的,固定的. 2. 不变值 同上 3. 运行时可以 ...

  3. springboot如何直接读取webapp下页面?

    公司改用springboot的时候,将页面相关的文件都放在了src/main/webapp下,我直接通过main方式启动的时候,无法读取到src/mian/webapp下文件,但是通过spring-b ...

  4. JMeter java.net.URISyntaxException: Illegal character in query at index

    请求参数未编码,会造成请求解析失败.把编码勾上,就可以了.

  5. 【BZOJ3190】[JLOI2013]赛车 单调栈+几何

    [BZOJ3190][JLOI2013]赛车 Description 这里有一辆赛车比赛正在进行,赛场上一共有N辆车,分别称为个g1,g2……gn.赛道是一条无限长的直线.最初,gi位于距离起跑线前进 ...

  6. [LintCode] 二叉树的前序遍历

    The recursive solution is trivial and I omit it here. Iterative Solution using Stack (O(n) time and ...

  7. screen命令在freebsd安装和使用

    安装 # cd /usr/ports/sysutils/screen # make install clean 使用 # screen //以下^A表示同按“Ctrl + A”键 # ^A c //C ...

  8. Oracle安装错误:File not found WFMLRSVCApp.ear

    oracle 11g安装过程中问题:找不到WFMLRSVCApp.ear 在 oracle 11gR2 64bit 安装到window 7 64位操作系统中,安装到53%时,提示找不到WFMLRSVC ...

  9. flume sink两种类型 file_rool 自定义sing com.mycomm.MySink even if there is only one event, the event has to be sent in an array

    mkdir /data/UnifiedLog/; cd /data/UnifiedLog/; wget http://mirror.bit.edu.cn/apache/flume/1.8.0/apac ...

  10. fun_action

    make an absolute URI from a relative one http://php.net/manual/en/function.header.php <?php /* Re ...