如何使用純CSS實現蝴蝶標本的展示框效果

來源:互聯網
上載者:User
本篇文章給大家帶來的內容是關於如何使用純CSS實現蝴蝶標本的展示框效果 ,有一定的參考價值,有需要的朋友可以參考一下,希望對你有所協助。

效果預覽

原始碼下載

每日前端實戰系列的全部原始碼請從 github 下載:

https://github.com/comehope/front-end-daily-challenges

代碼解讀

定義 dom,容器表示整隻蝴蝶,因為蝴蝶是對稱的,所以分為左右兩邊,每邊有 3 個子項目:

<div class="butterfly">    <div class="left">        <span></span>        <span></span>        <span></span>    </div>    <div class="right">        <span></span>        <span></span>        <span></span>    </div></div>

置中顯示:

body {    margin: 0;    height: 100vh;    display: flex;    align-items: center;    justify-content: center;    background: linear-gradient(gray, lightyellow, gray);}

定義蝴蝶的尺寸:

.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);}

大功告成!

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.