前端每日實戰:62# 視頻演示如何用純 CSS 創做一隻蒸鍋

圖片描述

效果預覽

按下右側的「點擊預覽」按鈕能夠在當前頁面預覽,點擊連接能夠全屏預覽。css

https://codepen.io/comehope/pen/YvOzNyhtml

可交互視頻

此視頻是能夠交互的,你能夠隨時暫停視頻,編輯視頻中的代碼。前端

請用 chrome, safari, edge 打開觀看。git

https://scrimba.com/p/pEgDAM/cg46aSGgithub

源代碼下載

每日前端實戰系列的所有源代碼請從 github 下載:chrome

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

代碼解讀

定義 dom,容器中包含 2 個元素,分別表示鍋蓋和鍋體:flex

<div class="steamer">
    <div class="lid"></div>
    <div class="pot"></div>
</div>

居中顯示:動畫

body {
    margin: 0;
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(to right bottom, violet, midnightblue);
}

定義容器尺寸:spa

.steamer {
    width: 30em;
    height: 30em;
    background-color: snow;
    font-size: 10px;
    border-radius: 50%;
}

畫出鍋體:

.steamer {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}

.pot {
    position: relative;
    width: 16em;
    height: 12em;
    background: darkslateblue;
    border-radius: 0.5em 0.5em 6.5em 6.5em;
    border-right: 1.5em solid midnightblue;
}

畫出鍋把手:

.steamer {
    z-index: 1;
}

.pot::before {
    content: '';
    position: absolute;
    width: 27em;
    height: 2.5em;
    background-color: tomato;
    left: -4.75em;
    top: 2em;
    z-index: -1;
}

畫出鍋蓋:

.lid {
    width: 17em;
    height: 6em;
    background-color: gold;
    position: relative;
    border-radius: 6em 6em 0 0;
    border-right: 1.5em solid goldenrod;

畫出鍋蓋上的鈕釦把手:

.lid::before {
    content: '';
    position: absolute;
    width: 4em;
    height: 4em;
    background-color: tomato;
    border-radius: 50%;
    left: 7em;
    top: -2.5em;
}

接下來潤色一下。

爲鍋體增長光影:

.pot::after {
    content: '';
    position: absolute;
    width: 8em;
    height: 8em;
    border: 0.6em solid transparent;
    border-radius: 50%;
    border-left-color: white;
    transform: rotate(-60deg);
    top: 1em;
    left: 2em;
}

爲鍋蓋增長光影:

.lid::after {
    content: '';
    position: absolute;
    width: 7em;
    height: 7em;
    border: 0.6em solid transparent;
    border-radius: 50%;
    border-left-color: white;
    transform: rotate(40deg);
    top: 0.6em;
    left: 2.5em;
}

最後,增長動畫效果:

.lid {
    transform: translateY(-0.5em);
    animation: animate 1s infinite alternate;
}

@keyframes animate {
    to {
        transform: translateY(0.5em);
    }
}

大功告成!

相關文章
相關標籤/搜索