使用純 CSS 創(chuàng)作一個(gè)漸變色動(dòng)畫邊框

效果預(yù)覽
按下右側(cè)的“點(diǎn)擊預(yù)覽”按鈕可以在當(dāng)前頁(yè)面預(yù)覽,點(diǎn)擊鏈接可以全屏預(yù)覽。
https://codepen.io/comehope/pen/odpRKX
可交互視頻教程
此視頻是可以交互的,你可以隨時(shí)暫停視頻,編輯視頻中的代碼。
請(qǐng)用 chrome, safari, edge 打開觀看。
源代碼下載
請(qǐng)從 github 下載。
代碼解讀
定義 dom,一個(gè)容器中包含一些文字:
<div class="box"> you are my<br> FAVORITE </div>
居中顯示:
html, body, .box { height: 100%; display: flex; align-items: center; justify-content: center; }
設(shè)置頁(yè)面背景色:
body { background: #222; }
設(shè)置容器和文字樣式:
.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; }
用偽元素增加一個(gè)背板:
.box { position: relative; } .box::after { content: ''; position: absolute; width: 102%; height: 104%; background-color: orange; z-index: -1; border-radius: 0.2em; }
把背板設(shè)置為漸變色的:
.box::after { /*background-color: orange;*/ background-image: linear-gradient(60deg, aquamarine, cornflowerblue, goldenrod, hotpink, salmon, lightgreen, sandybrown, violet); }
為背板設(shè)置動(dòng)畫效果:
.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; } }
大功告成!
知識(shí)點(diǎn)
•z-index https://developer.mozilla.org/en-US/docs/Web/CSS/z-index
•background-image https://developer.mozilla.org/en-US/docs/Web/CSS/background-image
•background-size https://developer.mozilla.org/en-US/docs/Web/CSS/background-size
•background-position https://developer.mozilla.org/en-US/docs/Web/CSS/background-position
總結(jié)
以上所述是小編給大家介紹的使用純 CSS 創(chuàng)作一個(gè)漸變色動(dòng)畫邊框,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
CSS3 按鈕邊框動(dòng)畫的實(shí)現(xiàn)
這篇文章主要介紹了CSS3 按鈕邊框動(dòng)畫的實(shí)現(xiàn),幫助大家更好的理解和使用CSS3,美化自身網(wǎng)頁(yè),感興趣的朋友可以了解下2020-11-12CSS 奇思妙想邊框動(dòng)畫效果的實(shí)現(xiàn)
這篇文章主要介紹了CSS 奇思妙想邊框動(dòng)畫效果的實(shí)現(xiàn),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-18