按下右側的「點擊預覽」按鈕能夠在當前頁面預覽,點擊連接能夠全屏預覽。css
https://codepen.io/comehope/pen/RYZbmEhtml
此視頻是能夠交互的,你能夠隨時暫停視頻,編輯視頻中的代碼。前端
請用 chrome, safari, edge 打開觀看。git
https://scrimba.com/p/pEgDAM/c7MPZtggithub
每日前端實戰系列的所有源代碼請從 github 下載:chrome
https://github.com/comehope/front-end-daily-challengesdom
定義 dom,容器中包含 2 個子元素,分別表明 1 個女生和一羣男生(4個),每一個 span
元素表明 1 我的(1 個方塊):ide
<figure class="container"> <span class="girl"></span> <div class="boys"> <span></span> <span></span> <span></span> <span></span> </div> </figure>
居中顯示:佈局
body { margin: 0; height: 100vh; display: flex; align-items: center; justify-content: center; }
定義容器尺寸和它的子元素佈局:flex
.container { width: 8em; height: 1em; font-size: 35px; display: flex; justify-content: space-between; }
畫出 5 個方塊,用邊框做爲輔助線幫助定位:
.container span { width: 1em; height: 1em; border: 1px dashed black; /* 輔助線 */ } .boys { width: 6em; display: flex; justify-content: space-between; }
用僞元素設置元素的樣式,使它們變得柔和一些,爲男生和男生填上不一樣的顏色,同時刪掉上一步的輔助線:
.container span::before { content: ''; position: absolute; width: inherit; height: inherit; border-radius: 15%; box-shadow: 0 0 0.2em rgba(0, 0, 0, 0.3); } .girl::before { background-color: hotpink; } .boys span::before { background-color: dodgerblue; }
使 4 個男生色塊的顏色逐漸變淡,增長一點層次感:
.boys span:nth-child(1)::before { filter: brightness(1); } .boys span:nth-child(2)::before { filter: brightness(1.15); } .boys span:nth-child(3)::before { filter: brightness(1.3); } .boys span:nth-child(4)::before { filter: brightness(1.45); }
接下來製做動畫效果。
先增長女生移動的效果,同時顏色也作漸淡處理,後面其餘動畫的時間要保持一致,因此把動畫時長設置爲變量:
.container span { width: 1em; height: 1em; --duration: 3s; } .girl { animation: slide var(--duration) ease-in-out infinite; } @keyframes slide { from { transform: translateX(0); filter: brightness(1); } to { transform: translatex(calc(8em - (1em * 1.25))); filter: brightness(1.45); } }
而後增長第 1 個男生跳開的動畫效果,注意從 15% 到 35% 旋轉的原點是在元素的正上方:
.boys span { animation: var(--duration) ease-in-out infinite; } .boys span:nth-child(1) { animation-name: jump-off-1; } @keyframes jump-off-1 { 0%, 15% { transform: rotate(0deg); } 35%, 100% { transform-origin: -50% center; transform: rotate(-180deg); } }
參考第 1 個男生的動畫效果,再增長另外 3 個男生跳開的動畫效果,區別只是調整了關鍵幀的時間,依次後延 15% 的時間:
.boys span:nth-child(2) { animation-name: jump-off-2; } .boys span:nth-child(3) { animation-name: jump-off-3; } .boys span:nth-child(4) { animation-name: jump-off-4; } @keyframes jump-off-2 { 0%, 30% { transform: rotate(0deg); } 50%, 100% { transform-origin: -50% center; transform: rotate(-180deg); } } @keyframes jump-off-3 { 0%, 45% { transform: rotate(0deg); } 65%, 100% { transform-origin: -50% center; transform: rotate(-180deg); } } @keyframes jump-off-4 { 0%, 60% { transform: rotate(0deg); } 80%, 100% { transform-origin: -50% center; transform: rotate(-180deg); } }
爲第 1 個男生增長擬人的動畫效果,這個效果寫在 ::before
僞元素中,動畫的過程是從正常到壓扁、而後抻長、再壓扁、最後恢復正常,注意從 25% 到 40% 的壓扁變形,由於此時主元素已經翻個兒,因此 transform-origin
的原點和 從 5% 到 15% 的壓扁變形的原點不同:
.boys span::before { animation: var(--duration) ease-in-out infinite; } .boys span:nth-child(1)::before { filter: brightness(1); animation-name: jump-down-1; } @keyframes jump-down-1 { 5% { transform: scale(1, 1); } 15% { transform-origin: center bottom; transform: scale(1.3, 0.7); } 20%, 25% { transform-origin: center bottom; transform: scale(0.8, 1.4); } 40% { transform-origin: center top; transform: scale(1.3, 0.7); } 55%, 100% { transform: scale(1, 1); } }
參考第 1 個男生 ::before
僞元素的動畫效果,再增長另外 3 個男生擬人的動畫效果,區別只是調整了關鍵幀的時間,依次後延 15% 的時間:
.boys span:nth-child(2)::before { animation-name: jump-down-2; } .boys span:nth-child(3)::before { animation-name: jump-down-3; } .boys span:nth-child(4)::before { animation-name: jump-down-4; } @keyframes jump-down-2 { 20% { transform: scale(1, 1); } 30% { transform-origin: center bottom; transform: scale(1.3, 0.7); } 35%, 40% { transform-origin: center bottom; transform: scale(0.8, 1.4); } 55% { transform-origin: center top; transform: scale(1.3, 0.7); } 70%, 100% { transform: scale(1, 1); } } @keyframes jump-down-3 { 35% { transform: scale(1, 1); } 45% { transform-origin: center bottom; transform: scale(1.3, 0.7); } 50%, 55% { transform-origin: center bottom; transform: scale(0.8, 1.4); } 70% { transform-origin: center top; transform: scale(1.3, 0.7); } 85%, 100% { transform: scale(1, 1); } } @keyframes jump-down-4 { 50% { transform: scale(1, 1); } 60% { transform-origin: center bottom; transform: scale(1.3, 0.7); } 65%, 70% { transform-origin: center bottom; transform: scale(0.8, 1.4); } 85% { transform-origin: center top; transform: scale(1.3, 0.7); } 100%, 100% { transform: scale(1, 1); } }
至此,女生從左側移動到右側的動畫效果已經完成。
把全部動畫屬性都加上 alternate
參數,使全部動畫都往復執行,就實現了從右側再回到左側的效果:
.girl { animation: slide var(--duration) ease-in-out infinite alternate; } .boys span { animation: var(--duration) ease-in-out infinite alternate; } .boys span::before { animation: var(--duration) ease-in-out infinite alternate; }
大功告成!