如何使用純CSS實現小球跳躍台階的動畫效果(附源碼)

來源:互聯網
上載者:User
本篇文章給大家帶來的內容是關於如何使用純CSS實現小球跳躍台階的動畫效果(附源碼) ,有一定的參考價值,有需要的朋友可以參考一下,希望對你有所協助。

效果預覽

原始碼下載

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

代碼解讀

定義 dom,容器中包含 5 個元素,代表 5 個台階:

<div class="loader">    <span></span>    <span></span>    <span></span>    <span></span>    <span></span></div>

置中顯示:

body {    margin: 0;    height: 100vh;    display: flex;    align-items: center;    justify-content: center;    background-color: black;}

定義容器尺寸:

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

畫出 5 個台階:

.loader {    display: flex;    justify-content: space-between;    align-items: flex-end;}.loader span {    width: 1em;    height: 1em;    background-color: white;}

用變數讓 5 個台階從低到高排序:

.loader span {    height: calc(var(--n) * 1em);}.loader span:nth-child(1) {    --n: 1;}.loader span:nth-child(2) {    --n: 2;}.loader span:nth-child(3) {    --n: 3;}.loader span:nth-child(4) {    --n: 4;}.loader span:nth-child(5) {    --n: 5;}

為台階增加轉換排序方向的動畫效果:

.loader span {    animation: sort 5s infinite;}@keyframes sort {    0%, 40%, 100% {        height: calc(var(--n) * 1em);    }    50%, 90% {        height: calc(5em - (var(--n) - 1) * 1em);    }}

下面做小球的動畫,用了障眼法,使 2 個同色小球的交替運動看起來就像 1 個小球在做往複運動。

用虛擬元素畫出 2 個小球:

.loader::before,.loader::after {    content: '';    position: absolute;    width: 1em;    height: 1em;    background-color: white;    border-radius: 50%;    bottom: 1em;}.loader::before {    left: 0;}.loader::after {    left: 6em;}

增加讓小球向上運動的動畫效果:

.loader::before,.loader::after {    animation: climbing 5s infinite;    visibility: hidden;}.loader::after {    animation-delay: 2.5s;}@keyframes climbing {    0% {        bottom: 1em;        visibility: visible;    }    10% {        bottom: 2em;    }    20% {        bottom: 3em;    }    30% {        bottom: 4em;    }    40% {        bottom: 5em;    }    50% {        bottom: 1em;    }    50%, 100% {        visibility: hidden;    }}

在向上運動的同時向兩側運動,形成上台階的動畫效果:

.loader::before {    --direction: 1;}.loader::after {    --direction: -1;}@keyframes climbing {    0% {        bottom: 1em;        left: calc(3em - 2 * 1.5em * var(--direction));        visibility: visible;    }    10% {        bottom: 2em;        left: calc(3em - 1 * 1.5em * var(--direction));    }    20% {        bottom: 3em;        left: calc(3em - 0 * 1.5em * var(--direction));    }    30% {        bottom: 4em;        left: calc(3em + 1 * 1.5em * var(--direction));    }    40% {        bottom: 5em;        left: calc(3em + 2 * 1.5em * var(--direction));    }    50% {        bottom: 1em;        left: calc(3em + 2 * 1.5em * var(--direction));    }    50%, 100% {        visibility: hidden;    }}

最後,為上台階的動作增加擬人效果:

@keyframes climbing {    0% {        bottom: 1em;        left: calc(3em - 2 * 1.5em * var(--direction));        visibility: visible;    }    7% {        bottom: calc(2em + 0.3em);    }    10% {        bottom: 2em;        left: calc(3em - 1 * 1.5em * var(--direction));    }    17% {        bottom: calc(3em + 0.3em);    }    20% {        bottom: 3em;        left: calc(3em - 0 * 1.5em * var(--direction));    }    27% {        bottom: calc(4em + 0.3em);    }    30% {        bottom: 4em;        left: calc(3em + 1 * 1.5em * var(--direction));    }    37% {        bottom: calc(5em + 0.3em);    }    40% {        bottom: 5em;        left: calc(3em + 2 * 1.5em * var(--direction));    }    50% {        bottom: 1em;        left: calc(3em + 2 * 1.5em * var(--direction));    }    50%, 100% {        visibility: hidden;    }}

大功告成!

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.