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

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