前端每日實戰:107# 視頻演示如何用純 CSS 創做一隻單眼怪獸

圖片描述

效果預覽

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

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

可交互視頻

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

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

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

源代碼下載

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

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

代碼解讀

定義 dom,容器中包含的子元素分別表示耳朵、眼睛和嘴,嘴裏還有牙齒:flex

<div class="monster">
    <span class="ear left"></span>
    <span class="ear right"></span>  
    <span class="eye"></span>
    <span class="mouth">
        <span class="tooth"></span>
    </span>
</div>

居中顯示:動畫

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

畫出怪獸的輪廓:spa

.monster {
    width: 10em;
    height: 13em;
    font-size: 16px;
    color: blueviolet;
    background-color: currentColor;
    border-radius: 5em 5em 0 0 / 6em 6em 0 0;
}

畫出眼睛的輪廓:

.monster {
    position: relative;
}

.eye {
    position: absolute;
    width: 6.5em;
    height: 6.5em;
    background: white;
    border-radius: 77% 77% 77% 77% / 92% 92% 60% 60%;
    top: 2em;
    left: calc((10em - 6.5em) / 2);
    box-shadow: 0.2em 0.9em 0 rgba(0, 0, 0, 0.1);
}

用徑向漸變畫出眼珠和瞳孔:

.eye {
    background: 
        radial-gradient(
            circle at 50% 25%,
            white 0.4em,
            transparent 0.4em
        ),
        radial-gradient(
            circle at 50% 40%,
            black 1.7em,
            transparent 1.7em
        ),
        white;
}

畫出嘴的輪廓:

.mouth {
    position: absolute;
    width: 3em;
    height: 2.1em;
    background-color: black;
    border-radius: 70% 70% 3.5em 3.5em;
    bottom: 0.9em;
    left: calc((10em - 3em) / 2);
}

用僞元素畫出舌頭:

.mouth {
    overflow: hidden;
}

.mouth::before {
    content: '';
    position: absolute;
    width: inherit;
    height: 0.6em;
    background-color: tomato;
    border-radius: 50% 50% 0 0;
    bottom: 0;
}

畫出三顆牙齒的中間那顆:

.tooth {
    position: absolute;
    border-top: 0.8em solid white;
    border-left: 0.4em solid transparent;
    border-right: 0.4em solid transparent;
    left: 1.1em;
}

用僞元素畫出三顆牙齒的左右兩顆:

.tooth::before,
.tooth::after {
    position: absolute;
    border-top: 0.8em solid white;
    border-left: 0.4em solid transparent;
    border-right: 0.4em solid transparent;
    left: 1.1em;
}

.tooth::before {
    content: '';
    left: -1.1em;
    top: -0.8em;
}

.tooth::after {
    content: '';
    left: 0.3em;
    top: -0.8em;
}

畫出耳朵的形狀:

.ear {
    position: absolute;
    width: 2.4em;
    height: 4.5em;
    top: -3em;
}

.ear::before {
    content: '';
    position: absolute;
    width: 0.9em;
    height: inherit;
    background-color: currentColor;
    left: calc((2.4em - 0.9em) / 2);
}

.ear::after {
    content: '';
    position: absolute;
    width: 2.4em;
    height: 2.4em;
    top: -0.5em;
    background-color: currentColor;
    border-radius: 50%;
    box-shadow: inset -0.3em -0.2em 0 rgba(0, 0, 0, 0.1);
}

分別定位左耳和右耳:

.ear {
    transform-origin: bottom center;
    transform: rotate(calc(10deg * var(--direction)));
}

.ear.left {
    left: 2em;
    --direction: -1;
}

.ear.right {
    right: 2em;
    --direction: 1;
}

接下來增長動畫效果:
增長身體輕微彈動的效果:

.monster {
    transform-origin: bottom center;
    animation: body-bounce 1s infinite;
}

@keyframes body-bounce {
    50% {
        transform: scaleX(1.03) scaleY(0.97);
    }
}

增長耳朵輕微顫動的效果:

.ear {
    animation: ears-shake 5s infinite;
}

@keyframes ears-shake {
    50% {
        transform: rotate(calc(20deg * var(--direction)));
    }
}

增長眨眼效果:

.eye {
    animation: eye-blink 5s infinite;
}

@keyframes eye-blink {
    0%, 6% {
        transform: scaleX(1) scaleY(1);
    }

    3% {
        transform: scaleX(1.03) scaleY(0.1);
    }
}

大功告成!

相關文章
相關標籤/搜索