前端每日實戰:59# 視頻演示如何用純 CSS 創做彩虹背景文字

圖片描述

效果預覽

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

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

可交互視頻

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

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

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

源代碼下載

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

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

代碼解讀

定義 dom,容器中有一段文本:dom

<p>thanks</p>

居中顯示:flex

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

定義文字樣式:動畫

p {
    font-size: 20vw;
    color: white;
    font-family: sans-serif;
    text-transform: uppercase;
    font-weight: bold;
}

設置彩虹背景:

p {
    background-image: linear-gradient(
        to right,
        orangered,
        orange,
        gold,
        lightgreen,
        cyan,
        dodgerblue,
        mediumpurple,
        hotpink,
        orangered);
    background-size: 110vw;
}

定義動畫效果:

p {
    animation: sliding 30s linear infinite;
}

@keyframes sliding {
    to {
        background-position: -2000vw;
    }
}

最後,把彩虹背景移到文字下面:

p {
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}

大功告成!

相關文章
相關標籤/搜索