按下右側的「點擊預覽」按鈕能夠在當前頁面預覽,點擊連接能夠全屏預覽。css
https://codepen.io/comehope/pen/ZRjGGyhtml
此視頻是能夠交互的,你能夠隨時暫停視頻,編輯視頻中的代碼。前端
請用 chrome, safari, edge 打開觀看。git
https://scrimba.com/p/pEgDAM/cLQPgSEgithub
每日前端實戰系列的所有源代碼請從 github 下載:chrome
https://github.com/comehope/front-end-daily-challengesdom
定義 dom,容器中包含 1 個元素表示壺體,其中再包含 1 個元素表示壺把手:flex
<div class="container"> <div class="pot"> <div class="handle"></div> </div> </div>
居中顯示:動畫
body { margin: 0; height: 100vh; display: flex; align-items: center; justify-content: center; background: linear-gradient(to right bottom, silver, dimgray); }
重定義盒模型:spa
*, *::before, *::after { box-sizing: border-box; }
定義容器尺寸:
.container { width: 150px; height: 150px; background-color: snow; border-radius: 50%; }
畫出壺的最大的部分:
.container { display: flex; align-items: center; justify-content: center; } .pot { width: 85px; height: 85px; background-color: deepskyblue; border-radius: 50%; }
用僞元素畫出壺的上半部分:
.pot { position: relative; } .pot::before { content: ''; position: absolute; width: 85px; height: 43px; background-color: hotpink; border-radius: 43px 43px 0 0; }
用僞元素畫出壺嘴:
.pot::after { content: ''; position: absolute; width: 43px; height: 10px; background-color: hotpink; left: 21px; top: -3px; }
畫出把手橫向的部分:
.pot .handle { width: 83px; height: 7px; background-color: black; border-radius: 7px; position: absolute; left: 13px; top: 12px; }
用僞元素畫出把手豎向的部分:
.pot .handle::before { content: ''; position: absolute; width: 7px; height: 50px; background-color: black; border-radius: 7px; left: calc(85px - 7px); }
接下來潤色一下。
給壺加上陰影:
.pot { border-right: 5px solid steelblue; } .pot::before { border-right: 5px solid palevioletred; }
再用僞元素給壺加上光影:
.container::after { content: ''; position: absolute; width: 70px; height: 70px; border: 3px solid transparent; border-left-color: white; border-radius: 50%; left: 40px; top: 40px; transform: rotate(-10deg); }
最後,加上動畫:
.container { animation: pouring 3s linear infinite alternate; } @keyframes pouring { 0%, 25% { transform: rotate(0deg); } 75%, 100% { transform: rotate(-45deg); } }
大功告成!