前端每日實戰:76# 視頻演示如何用純 CSS 創做一組單元素辦公用品(內含2個視頻)

圖片描述

效果預覽

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

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

可交互視頻

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

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

上部:
https://scrimba.com/p/pEgDAM/cEb3nhKgithub

下部:
https://scrimba.com/p/pEgDAM/cqwzqARchrome

源代碼下載

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

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

代碼解讀

定義 dom,容器包含 5 個元素,分別表明紙張、尺子、記事本、日曆和鉛筆。spa

<div class="desk">
    <span class="paper"></span>
    <span class="ruler"></span>  
    <span class="notebook"></span>
    <span class="calendar"></span>
    <span class="pencil"></span>
</div>

居中顯示:code

body {
    margin: 0;
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
}

定義容器尺寸:

.desk {
    width: 70em;
    height: 70em;
    font-size: 5px;
    position: relative;
}

定義各元素的共有屬性:

.desk {
    --b: 1em solid darkslategray;
}

.desk * {
    position: absolute;
    border: var(--b);
    box-sizing: border-box;
}

.desk *::before,
.desk *::after {
    content: '';
    position: absolute;
    box-sizing: border-box;
}

畫出紙張的輪廓:

.paper {
    width: 24em;
    height: 30em;
    color: skyblue;
    background-color: currentColor;
    border-radius: 0 0 0 3em;
    top: 14em;
    left: 4em;
}

畫出紙張左側捲曲的部分:

.paper::before {
    width: 4em;
    height: 32em;
    background-color: currentColor;
    border: var(--b);
    filter: saturate(150%) brightness(0.9);
    bottom: 1.6em;
    left: -1em;
    border-radius: 3em 0 3em 0;
}

.paper::after {
    width: 4em;
    height: 5em;
    background-color: currentColor;
    border: var(--b);
    bottom: -1em;
    left: -1em;
    border-radius: 2.5em 0 0 2.5em;
    border-right: none;
}

畫出尺子的輪廓:

.ruler {
    width: 6em;
    height: 24em;
    background: linear-gradient(
        to right,
        peru 30%, 
        sandybrown 30%
    );
    top: 8em;
    left: 14em;
    transform: rotate(25deg);
}

畫出尺子上的長刻度線:

.ruler::before {
    width: 2em;
    height: calc(100% - 3em);
    background-image: linear-gradient(darkslategray 1em, transparent 0);
    background-size: 4em 4em;
    top: 1em;
}

畫出尺子上的短刻度線:

.ruler::after {
    width: 1em;
    height: calc(100% - 3em);
    background-image: linear-gradient(darkslategray 1em, transparent 0);
    background-size: 4em 4em;
    top: 3em;
}

畫出日曆的輪廓:

.calendar {
    width: 28em;
    height: 28em;
    background: linear-gradient(coral 30%, white 30%);
    border-radius: 2em;
    top: 14em;
    right: 4em;
    transform: rotate(15deg);
}

畫出日曆頂部的轉軸:

.calendar::before {
    width: 3em;
    height: 4em;
    color: sandybrown;
    background-color: currentColor;
    top: -2em;
    left: 4em;
    border-radius: 100% 100% 50% 50%;
    box-shadow:
        0 0, 0 0 0 1em darkslategray,
        15em 0, 15em 0 0 1em darkslategray;
}

畫出日曆上的日期:

.calendar::after {
    width: 3em;
    height: 3em;
    color: silver;
    background-color: silver;
    top: 10em;
    left: 4em;
    border-radius: 0.4em;
    box-shadow:
        0 0, 5em 0, 10em 0, 15em 0,
        0 5em, 5em 5em, 10em 5em, 15em 5em,
        0 10em, 5em 10em, 10em 10em, 15em 10em;
}

畫出記事本的輪廓:

.notebook {
    width: 26em;
    height: 34em;
    background: linear-gradient(
        to right,
        tomato 12%,
        coral 12%, coral 60%,
        darkslategray 60%, darkslategray 64%,
        sandybrown 64%, sandybrown 78%,
        darkslategray 78%, darkslategray 82%,
        coral 82%
    );
    border-radius: 3em;
    bottom: 8em;
    left: 20em;
}

畫出記事本的底部:

.notebook::before {
    width: inherit;
    height: 5em;
    background-color: white;
    border: var(--b);
    bottom: -1em;
    left: -1em;
    border-radius: 3em 0 3em 3em;
}

畫出記事本的書籤:

.notebook::after {
    width: 4em;
    height: 5em;
    background-color: coral;
    border: var(--b);
    bottom: -3em;
    left: 4em;
    border-top: none;
    border-radius: 0 0 1em 1em;
}

畫出鉛筆的輪廓:

.pencil {
    width: 6em;
    height: 12em;
    background: linear-gradient(
        coral 20%,
        darkslategray 20%, darkslategray 25%,
        sandybrown 25%
    );
    border-radius: 0.8em;
    bottom: 14em;
    right: 13em;
    transform: rotate(-35deg);
}

畫出鉛筆的筆尖:

.pencil::before {
    border: 3em solid transparent;
    border-top-color: darkslategray;
    bottom: -6.5em;
    left: -1em;
}

.pencil::after {
    border: 2em solid transparent;
    border-top-color: sandybrown;
    bottom: -4.5em;
    left: 0;
}

增長鼠標懸停效果,把物品都推向桌面的邊緣:

.desk * {
    transition: 1s;
}

.desk:hover .paper {
    top: 0;
    left: 0;
}

.desk:hover .ruler {
    top: 0;
    left: 50%;
    transform: rotate(0);
}

.desk:hover .calendar {
    top: 0;
    right: 0;
    transform: rotate(0deg);
}

.desk:hover .notebook {
    bottom: 0;
    left: 0;
}

.desk:hover .pencil {
    bottom: 0;
    right: 0;
    transform: rotate(180deg);
}

dom 中增長文字 「Hey, Take it easy」:

<div class="desk">
    <span class="paper"></span>
    <span class="ruler"></span>  
    <span class="calendar"></span>
    <span class="notebook"></span>
    <span class="pencil"></span>
    <p>Hey!<br>Take it easy!</p>
</div>

設置文字樣式:

.desk p {
    border: none;
    color: darkslategray;
    font-size: 6em;
    font-weight: bold;
    font-family: sans-serif;
    text-align: center;
    bottom: 2em;
    right: 1em;
}

最後,爲文字增長慢慢浮現的效果:

.desk p {
    transform: rotate(-35deg) scale(0);
}

.desk:hover p {
    transform: rotate(-35deg) scale(1);
}

大功告成!

相關文章
相關標籤/搜索