按下右側的「點擊預覽」按鈕在當前頁面預覽,點擊連接全屏預覽。css
https://codepen.io/zhang-ou/pen/xjXxozhtml
此視頻是能夠交互的,你能夠隨時暫停視頻,編輯視頻中的代碼。git
請用 chrome, safari, edge 打開觀看。github
https://scrimba.com/c/cBm4eU9chrome
請從 github 下載。dom
https://github.com/comehope/front-end-daily-challenges/tree/master/013-hot-coffee-cupflex
定義 dom,一個名爲 coffee 的容器,其中包含一個名爲 cup 的元素:動畫
<div class="coffee"> <div class="cup"></div> </div>
居中顯示:spa
html, body { height: 100%; align-items: center; justify-content: center; background-color: brown; }
畫出杯子主體:code
.coffee .cup { width: 10em; height: 9em; background-color: white; border-bottom-left-radius: 1.5em; border-bottom-right-radius: 1.5em; }
用僞元素畫出杯口:
.coffee .cup { position: relative; } .coffee .cup::before { content: ''; position: absolute; width: 100%; height: 2em; background-color: chocolate; border: 0.5em solid white; box-sizing: border-box; border-radius: 50%; top: -1em; box-shadow: inset 0 0 1em rgba(0, 0, 0, 0.5); }
用僞元素畫出杯子把手:
.coffee .cup::after { content: ''; position: absolute; width: 3em; height: 3.5em; border: 0.8em solid white; border-radius: 50%; top: 20%; left: 80%; }
dom 元素增長托盤:
<div class="coffee"> <div class="cup"></div> <div class="plate"></div> </div>
畫出托盤:
.coffee { display: flex; flex-direction: column; align-items: center; height: calc(9em + 1em); position: relative; } .coffee .plate { width: 16em; height: 1em; background-color: white; border-bottom-left-radius: 50%; border-bottom-right-radius: 50%; position: absolute; bottom: -1px; }
dom 元素增長杯中冒出的熱氣:
<div class="coffee"> <div class="vapor"> <span></span> <span></span> <span></span> <span></span> <span></span> </div> <div class="cup"></div> <div class="plate"></div> </div>
畫出杯中冒出的熱氣:
.coffee { height: calc(9em + 1em + 2em); } .coffee .vapor { width: 7em; display: flex; justify-content: space-between; } .coffee .vapor span { width: 0.1em; min-width: 1px; height: 2em; background-color: white; }
定義熱氣冒出的動畫:
.coffee .vapor span { animation: evaporation 2s linear infinite; filter: opacity(0); } @keyframes evaporation { from { transform: translateY(0); filter: opacity(1) blur(0.2em); } to { transform: translateY(-4em); filter: opacity(0) blur(0.4em); } }
最後,調整每條熱氣的延遲時間,使動感更強:
.coffee .vapor span:nth-child(1) { animation-delay: 0.5s; } .coffee .vapor span:nth-child(2) { animation-delay: 0.1s; } .coffee .vapor span:nth-child(3) { animation-delay: 0.3s; } .coffee .vapor span:nth-child(4) { animation-delay: 0.4s; } .coffee .vapor span:nth-child(5) { animation-delay: 0.2s; }
大功告成!