前端每日實戰:16# 視頻演示如何用純 CSS 創做一個漸變色動畫邊框

圖片描述

效果預覽

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

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

可交互視頻教程

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

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

https://scrimba.com/c/cmQV7Hdchrome

源代碼下載

請從 github 下載。dom

https://github.com/comehope/front-end-daily-challenges/tree/master/016-colorful-gradient-animated-borderflex

代碼解讀

定義 dom,一個容器中包含一些文字:動畫

<div class="box">
    you are my<br>
    FAVORITE
</div>

居中顯示:spa

html,
body,
.box {
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
}

設置頁面背景色:code

body {
    background: #222;
}

設置容器和文字樣式:

.box {
    color: white;
    font-size: 2.5em;
    width: 10em;
    height: 5em;
    background: #111;
    font-family: sans-serif;
    line-height: 1.5em;
    text-align: center;
    border-radius: 0.2em;
}

用僞元素增長一個背板:

.box {
    position: relative;
}

.box::after {
    content: '';
    position: absolute;
    width: 102%;
    height: 104%;
    background-color: orange;
    z-index: -1;
    border-radius: 0.2em;
}

把背板設置爲漸變色的:

.box::after {
    /*background-color: orange;*/
    background-image: linear-gradient(60deg, aquamarine, cornflowerblue, goldenrod, hotpink, salmon, lightgreen, sandybrown, violet);
}

爲背板設置動畫效果:

.box::after {
    background-size: 300%, 300%;
    animation: animate_bg 5s ease infinite alternate;
}

@keyframes animate_bg {
    0% {
        background-position: 0%, 50%;
    }

    50% {
        background-position: 100%, 50%;
    }

    100% {
        background-position: 0%, 50%;
    }
}

最後,再爲文字增長變色效果:

.box {
    animation: animate_text 2s linear infinite alternate;
}

@keyframes animate_text {
    from {
        color: lime;
    }

    to {
        color: yellow;
    }
}

大功告成!

知識點

相關文章
相關標籤/搜索