前端每日實戰:33# 視頻演示如何用純 CSS 創做牛奶文字變換效果

圖片描述

效果預覽

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

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

可交互視頻教程

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

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

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

源代碼下載

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

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

代碼解讀

定義 DOM,容器中包含 2 段文本:字體

<div class="container">
    <p>Explorer</p>
    <p>Discovery</p>
</div>

居中顯示:flex

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

設置字體樣式:動畫

p {
    color: white;
    font-size: 100px;
    font-weight: bold;
    font-family: sans-serif;
    text-transform: uppercase;
    text-align: center;
}

讓 2 段文本重疊:

p {
    margin: 0;
}

p:nth-child(1) {
    transform: translateY(50%);
}

p:nth-child(2) {
    transform: translateY(-50%);
}

定義動畫,讓 2 段文本交替顯示:

p {
    animation: show-hide 10s infinite;
    filter: opacity(0);
}

p:nth-child(1) {
    animation-direction: normal;
}

p:nth-child(2) {
    animation-direction: reverse;
}

@keyframes show-hide {
    0% {
        filter: opacity(0);
    }

    25% {
        filter: opacity(1);
    }

    40% {
        filter: opacity(1);
    }

    50% {
        filter: opacity(0);
    }
}

增長字間距的變化效果:

@keyframes show-hide {
    0% {
        filter: opacity(0);
        letter-spacing: -0.8em;
    }

    25% {
        filter: opacity(1);
    }

    40% {
        filter: opacity(1);
    }

    50% {
        filter: opacity(0);
        letter-spacing: 0.24em;
    }
}

增長文本模糊效果:

@keyframes show-hide {
    0% {
        filter: opacity(0) blur(0.08em);
        letter-spacing: -0.8em;
    }

    25% {
        filter: opacity(1) blur(0.08em);
    }

    40% {
        filter: opacity(1) blur(0.24em);
    }

    50% {
        filter: opacity(0) blur(0.24em);
        letter-spacing: 0.24em;
    }
}

最後,爲容器設置對比度濾鏡:

.container {
    filter: contrast(10);
    background-color: black;
    overflow: hidden;
}

大功告成!

相關文章
相關標籤/搜索