https://github.com/comehope/front-end-daily-challenges
定義 dom,容器中包含 2 個元素,分別代表怪獸的身體和眼睛:
<p class="monster"> <span class="body"></span> <span class="eyes"></span> </p>
設(shè)置背景色:
body { margin: 0; height: 100vh; background-color: black; }
設(shè)置前景色:
.monster { width: 100vw; height: 50vh; background-color: lightcyan; }
畫出怪獸的身體:
.monster { position: relative; } .body { position: absolute; width: 32vmin; height: 32vmin; background-color: teal; border-radius: 43% 40% 43% 40%; bottom: calc(-1 * 32vmin / 2 - 4vmin); }
定義怪獸眼睛所在的容器:
.eyes { width: 24vmin; height: 5vmin; position: absolute; bottom: 2vmin; left: calc(32vmin - 24vmin - 2vmin); }
用偽元素畫出怪獸的眼睛:
.eyes::before, .eyes::after { content: ''; position: absolute; width: 5vmin; height: 5vmin; border: 1.25vmin solid white; box-sizing: border-box; border-radius: 50%; } .eyes::before { left: 4vmin; } .eyes::after { right: 4vmin; }
為怪獸定義輕輕跳起的動畫,結(jié)合后面的動畫效果,讓它具有果凍的彈性:
.body { animation: bounce 1s infinite alternate; } @keyframes bounce { to { bottom: calc(-1 * 32vmin / 2 - 2vmin); } }
讓怪獸的身體轉(zhuǎn)動起來:
@keyframes wave { to { transform: rotate(360deg); } }
讓怪獸徘徊行走:
.monster { overflow: hidden; } .body { left: -2vmin; animation: wander 5s linear infinite alternate, wave 3s linear infinite, bounce 1s infinite alternate; } .eyes { animation: wander 5s linear infinite alternate; } @keyframes wander { to { left: calc(100% - 32vmin + 2vmin); } }
最后,讓怪獸的眼睛眨一眨:
.eyes::before, .eyes::after { animation: blink 3s infinite linear; } @keyframes blink { 4%, 10%, 34%, 40% { transform: scaleY(1); } 7%, 37% { transform: scaleY(0); } }
大功告成!
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:0731-84117792 E-MAIL:11247931@qq.com