前端每日實戰:63# 視頻演示如何用純 CSS 創做一臺烤麪包機

圖片描述

效果預覽

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

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

可交互視頻

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

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

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

源代碼下載

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

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

代碼解讀

定義 dom,容器中包含 5 個元素,分別表明機體、按鈕、支腿、手柄和麪包。flex

<div class="toaster">
    <div class="body"></div>
    <div class="button"></div>
    <div class="legs"></div>
    <div class="handle"></div>
    <div class="toast"></div>
</div>

居中顯示:動畫

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

定義容器尺寸:spa

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

畫出機體:

.toaster {
    position: relative;
}

.body {
    width: 16em;
    height: 14em;
    background-color: seagreen;
    position: absolute;
    top: 10em;
    left: 6em;
    border-radius: 2.5em;
    border-right: 1.5em solid darkgreen;
}

畫出按鈕:

.button {
    width: 2.5em;
    height: 2.5em;
    background-color: tomato;
    position: absolute;
    top: 13em;
    left: 16em;
    border-radius: 50%;
}

畫出支腿:

.legs::before,
.legs::after {
    content: '';
    position: absolute;
    width: 1.5em;
    height: 2em;
    background: tomato;
    top: 24em;
}

.legs::before {
    left: 9em;
}

.legs::after {
    right: 10em;
}

畫出手柄:

.handle {
    width: 4.2em;
    height: 1.8em;
    background-color: tomato;
    position: absolute;
    top: 12em;
    right: 2.4em;
    border-radius: 0 0.6em 0.6em 0;
}

畫出麪包:

.toaster {
    z-index: 1;
}

.toast {
    width: 9em;
    height: 6em;
    background-color: gold;
    position: absolute;
    top: 4em;
    left: 10em;
    border-radius: 2em 2em 0 0;
    border-right: 0.6em solid goldenrod;
    z-index: -1;
}

爲機體增長一些光影:

.body::before,
.body::after {
    content: '';
    position: absolute;
    width: 5em;
    height: 5em;
    border: 0.6em solid transparent;
    border-radius: 50%;
    border-left-color: white;
}

.body::before {
    transform: rotate(40deg);
    top: 1em;
    left: 1em;
}

.body::after {
    bottom: 1em;
    right: 1em;
    transform: rotate(210deg);
}

定義動畫效果:

@keyframes bake {
    0%, 20% {
        transform: translateY(0);
    }

    80%, 100% {
        transform: translateY(6em);
    }
}

最後,把動畫效果應用到手柄和麪包上:

.handle {
    animation: bake 3s infinite alternate;
}

.toast {
    animation: bake 3s infinite alternate;
}

大功告成!

相關文章
相關標籤/搜索