前端每日實戰:18# 視頻演示如何用純 CSS 創做 404 文字變形爲 NON 文字的交互特效

圖片描述

效果預覽

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

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

可交互視頻教程

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

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

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

源代碼下載

請從 github 下載。dom

https://github.com/comehope/front-end-daily-challenges/tree/master/018-stroke-morphing-404-effects佈局

代碼解讀

定義 dom,容器中包含 3 個 <p>,每一個 <p> 表明 1 個數字;每一個 p 標籤包含若干 <span>,每一個 <span> 表明 1 個筆劃:flex

<section class="four-zero-four">
    <p class="four">
        <span></span>
        <span></span>
        <span></span>
    </p>
    <p class="zero">
        <span></span>
        <span></span>
        <span></span>
        <span></span>
    </p>
    <p class="four">
        <span></span>
        <span></span>
        <span></span>
    </p>
</section>

居中顯示:spa

html, body {
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(gray, silver);
}

總體佈局:3d

.four-zero-four p {
    width: 10em;
    height: 10em;
    border: 1px dashed white;
    display: inline-block;
    margin: 1em;
    position: relative;
}

設置筆劃共有屬性:

.four-zero-four p span {
    position: absolute;
    box-sizing: border-box;
    filter: opacity(0.8);
}

畫出數字 4 的筆劃:

.four span:nth-child(1) {
    width: 20%;
    height: 80%;
    left: 10%;
}

.four span:nth-child(2) {
    width: 100%;
    height: 20%;
    bottom: 30%;
}

.four span:nth-child(3) {
    width: 20%;
    height: 100%;
    right: 10%;
}

畫出數字 0 的筆劃:

.zero span:nth-child(1) {
    width: 20%;
    height: 100%;
    left: 10%;
}

.zero span:nth-child(2) {
    width: 100%;
    height: 20%;
    top: 10%;
}

.zero span:nth-child(3) {
    width: 20%;
    height: 100%;
    right: 10%;
}

.zero span:nth-child(4) {
    width: 100%;
    height: 20%;
    bottom: 10%;
}

給筆劃上色:

.four span:nth-child(1) {
    background-color: yellowgreen;
}

.four span:nth-child(2) {
    background-color: turquoise;
}

.four span:nth-child(3) {
    background-color: pink;
}

.zero span:nth-child(1) {
    background-color: skyblue;
}

.zero span:nth-child(2) {
    background-color: plum;
}

.zero span:nth-child(3) {
    background-color: lightcoral;
}

.zero span:nth-child(4) {
    background-color: peachpuff;
}

設置劃過數字時筆劃的變化效果:

.four-zero-four p:hover span {
    border: 1px solid black;
    background-color: transparent;
    filter: opacity(1);
    transition: 0.3s;
}

設置劃過數字時筆劃的偏移量:

.four:hover span:nth-child(1) {
    left: 0;
}

.four:hover span:nth-child(2) {
    bottom: 0;
}

.four:hover span:nth-child(3) {
    right: 0;
}

.zero:hover span:nth-child(1) {
    left: 0;
}

.zero:hover span:nth-child(2) {
    top: 0;
}

.zero:hover span:nth-child(3) {
    right: 0;
}

.zero:hover span:nth-child(4) {
    bottom: 0;
}

最後,設置緩動時長:

.four-zero-four p span {
    transition: 0.3s;
}

.four-zero-four p:hover span {
    transition: 0.3s;
}

大功告成!

知識點

相關文章
相關標籤/搜索