按下右側的「點擊預覽」按鈕能夠在當前頁面預覽,點擊連接能夠全屏預覽。css
https://codepen.io/comehope/pen/xzgZzQhtml
此視頻是能夠交互的,你能夠隨時暫停視頻,編輯視頻中的代碼。前端
請用 chrome, safari, edge 打開觀看。git
https://scrimba.com/p/pEgDAM/cN7EncLgithub
每日前端實戰系列的所有源代碼請從 github 下載:chrome
https://github.com/comehope/front-end-daily-challengesdom
定義 dom,容器表示整隻蝴蝶,由於蝴蝶是對稱的,因此分爲左右兩邊,每邊有 3 個子元素:flex
<div class="butterfly"> <div class="left"> <span></span> <span></span> <span></span> </div> <div class="right"> <span></span> <span></span> <span></span> </div> </div>
居中顯示:url
body { margin: 0; height: 100vh; display: flex; align-items: center; justify-content: center; background: linear-gradient(gray, lightyellow, gray); }
定義蝴蝶的尺寸:spa
.butterfly { position: relative; width: 10em; height: 10em; }
先畫左半邊:
.butterfly .left { position: absolute; width: inherit; height: inherit; }
用第 1 個子元素畫出翅膀的上半部分:
.butterfly span { position: absolute; border-radius: 50%; } .butterfly span:nth-child(1) { width: 5em; height: 7em; background-color: gold; }
用第 2 個子元素畫出翅膀的下半部分:
.butterfly span:nth-child(2) { width: 5.5em; height: 3.5em; background-color: orangered; top: 5em; left: -2.5em; filter: opacity(0.6); }
用第 3 個子元素畫出觸角:
.butterfly span:nth-child(3) { width: 6em; height: 6em; border-right: 0.3em solid orangered; top: -0.5em; }
把左半邊複製一份到右半邊:
.butterfly .right { position: absolute; width: inherit; height: inherit; } .butterfly .right { transform: rotateY(180deg) rotate(-90deg); top: 0.4em; left: 0.4em; }
把標本裝到展現框裏:
.butterfly::before { content: ''; position: absolute; box-sizing: border-box; top: -2.5em; left: -8em; width: 24em; height: 18em; background-color: black; border: 0.2em inset silver; } .butterfly::after { content: ''; position: absolute; box-sizing: border-box; width: 40em; height: 30em; background-color: lightyellow; top: -9em; left: -16em; border: 2em solid burlywood; border-radius: 3em; box-shadow: 0 0.3em 2em 0.4em rgba(0, 0, 0, 0.3), inset 0.4em 0.4em 0.1em 0.5em rgba(0, 0, 0, .4); z-index: -1; }
最後,調整一下因圖案傾斜引發的位移:
.butterfly { transform: translateX(1em); }
大功告成!