前端每日實戰:74# 視頻演示如何用純 CSS 創做一臺 MacBook Pro

圖片描述

效果預覽

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

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

可交互視頻

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

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

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

源代碼下載

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

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

代碼解讀

定義 dom,容器中包含 2 個元素,分別表明屏幕和底座:flex

<div class="macbook">
    <span class="screen"></span>
    <span class="base"></span>
</div>

居中顯示:spa

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

定義容器尺寸:code

.macbook {
    width: 50em;
    font-size: 10px;
    display: flex;
    flex-direction: column;
    align-items: center;
}

畫出屏幕的輪廓:

.screen {
    width: 40em;
    height: calc(40em * 0.667);
    background-color: black;
    border-radius: 3% 3% 0 0 / 5%;
    border: 0.2em solid silver;
    border-bottom: none;
    position: relative;
}

畫出屏幕上的光影:

.screen {
    position: relative;
}

.screen::before {
    content: '';
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    background: radial-gradient(
        circle at right bottom,
        rgba(255, 255, 255, 0.4) 75%,
        rgba(255, 255, 255, 0.6) 75%
    );
    margin: 4.3% 3.2%;
}

畫出底座的輪廓:

.base {
    position: relative;
}

.base {
    width: inherit;
    height: 1.65em;
    background: linear-gradient(
        white,
        white 55%,
        #999 60%,
        #222 90%,
        rgba(0, 0, 0, 0.1) 100%
    );
    border-radius: 0 0 10% 10% / 0 0 50% 50%;
}

爲底座增長光照效果:

.base::before {
    content: '';
    position: absolute;
    width: inherit;
    height: 55%;
    background: linear-gradient(
        to right,
        rgba(0, 0, 0, 0.5) 0%,
        rgba(255, 255, 255, 0.8) 1%,
        rgba(0, 0, 0, 0.4) 4%,
        transparent 15%,
        rgba(255, 255, 255, 0.8) 50%,
        transparent calc(100% - 15%),
        rgba(0, 0, 0, 0.4), calc(100% - 4%),
        rgba(255, 255, 255, 0.8) calc(100% - 1%),
        rgba(0, 0, 0, 0.5) 100%
    );
}

畫出底座上用於掀開屏幕的缺口:

.base::after {
    content: '';
    position: absolute;
    width: 7em;
    height: 0.7em;
    background-color: #ddd;
    left: calc(50% - 7em / 2);
    box-shadow: 
        inset -0.5em -0.1em 0.3em rgba(0, 0, 0, 0.2),
        inset 0.5em 0.1em 0.3em rgba(0, 0, 0, 0.2);
    border-radius: 0 0 7% 7% / 0 0 95% 95%;
}

大功告成!

相關文章
相關標籤/搜索