按下右側的「點擊預覽」按鈕能夠在當前頁面預覽,點擊連接能夠全屏預覽。css
https://codepen.io/comehope/pen/EpWagMhtml
此視頻是能夠交互的,你能夠隨時暫停視頻,編輯視頻中的代碼。前端
請用 chrome, safari, edge 打開觀看。git
https://scrimba.com/p/pEgDAM/cZwGRTLgithub
每日前端實戰系列的所有源代碼請從 github 下載:chrome
https://github.com/comehope/front-end-daily-challengesdom
定義 dom,只有 1 個元素:flex
<div class="fridge"></div>
居中顯示:spa
body { margin: 0; height: 100vh; display: flex; align-items: center; justify-content: center; background: linear-gradient(floralwhite, wheat); }
定義容器尺寸:code
.fridge { width: 24em; height: 38em; font-size: 10px; }
畫出冰箱的輪廓:
.fridge { border-top: 1em solid #333; border-bottom: 1em solid #333; background: silver; border-radius: 4em / 0.5em; box-shadow: 0 0.5em 1em -0.5em rgba(0, 0, 0, 0.5); }
用線性漸變畫出冰箱的金屬表面:
.fridge { background: linear-gradient( to right, darkgray, lightgray 10%, silver 40%, silver 80%, darkgray 90%, dimgray ); }
再用線性漸變畫出冰箱的把手:
.fridge { background: linear-gradient( to right, transparent 40%, gray 40%, silver 41%, dimgray 43%, transparent 43%, transparent 48%, gray 48%, silver 49%, dimgray 51%, transparent 51% ) no-repeat center / 100% 90%, /* handles */ linear-gradient( to right, darkgray, lightgray 10%, silver 40%, silver 80%, darkgray 90%, dimgray ); }
在冰箱表面的線性漸變中加畫門縫:
.fridge { background: linear-gradient( to right, transparent 40%, gray 40%, silver 41%, dimgray 43%, transparent 43%, transparent 48%, gray 48%, silver 49%, dimgray 51%, transparent 51% ) no-repeat center / 100% 90%, /* handles */ linear-gradient( to right, darkgray, lightgray 10%, silver 40%, silver 45%, black 45%, black 46%, silver 46%, silver 80%, darkgray 90%, dimgray ); }
用 before 僞元素畫出飲水機的輪廓:
.fridge::before { content: ''; position: absolute; width: 6em; height: 7.5em; background: black; top: calc((100% - 7.5em) / 2); left: 2.5em; }
用線性漸變畫出飲水機的擋板,用徑向漸變畫出飲水機的出水口:
.fridge::before { background: radial-gradient( circle at 50% 22%, black, black 1em, transparent 1em ), linear-gradient( transparent 25%, #222 25% ) no-repeat center top / 94% 98%, black; }
再用徑向漸變畫出 4 個 led 燈:
.fridge::before { background: radial-gradient( circle at 88% 10%, limegreen, limegreen 0.2em, transparent 0.2em ), radial-gradient( circle at 75% 10%, hotpink, hotpink 0.2em, transparent 0.2em ), radial-gradient( circle at 25% 10%, dodgerblue, dodgerblue 0.2em, transparent 0.2em ), radial-gradient( circle at 12% 10%, gold, gold 0.2em, transparent 0.2em ), radial-gradient( circle at 50% 22%, black, black 1em, transparent 1em ), linear-gradient( transparent 25%, #222 25% ) no-repeat center top / 94% 98%, black; }
用 after 僞元素畫出杯子的輪廓:
.fridge::after { content: ''; position: absolute; width: 2em; height: 3em; background: deepskyblue; bottom: calc((100% - 7.5em) / 2); left: calc(2.5em + (6em - 2em) / 2); }
稍稍修整杯子的形狀,使其頂大底小:
.fridge::after { clip-path: polygon(0 0, 100% 0, 90% 100%, 10% 100%); }
讓杯子頂部的顏色變淺,看起來好像杯裏有水的樣子:
.fridge::after { background: linear-gradient( to bottom, lightskyblue 25%, transparent 25% ), deepskyblue; }
最後,給水裏加些氣泡:
.fridge::after { background: radial-gradient( circle at 35% 40%, lightblue, lightblue 0.1em, transparent 0.4em ), radial-gradient( circle at 65% 60%, lightblue, lightblue 0.1em, transparent 0.3em ), radial-gradient( circle at 45% 80%, lightblue, lightblue 0.1em, transparent 0.2em ), linear-gradient( to bottom, lightskyblue 25%, transparent 25% ), deepskyblue; }
大功告成!