在之前的文章《利用CSS3創(chuàng)建炫酷的三角背景圖像》中,我們給大家介紹了一種創(chuàng)建炫酷三角背景圖像的方法,感興趣的朋友可以去了解一下~
而下面本文再給大家介紹一種創(chuàng)建炫酷背景圖像方法,帶大家了解一下如何利用CSS3創(chuàng)建變色背景圖像動(dòng)畫(huà),讓你的網(wǎng)頁(yè)更吸引人!
我們先來(lái)看看效果圖
下面我們來(lái)研究一下是怎么實(shí)現(xiàn)這個(gè)效果的:
首先我們不創(chuàng)建標(biāo)簽,直接在body標(biāo)簽上設(shè)置背景圖片
body { background-image: url("https://img.php.cn/upload/article/000/000/024/612360451cede816.jpg"); background-size: cover; background-repeat: no-repeat; background-attachment: fixed; background-position: center; }
怎么將圖片變色呢?這就需要在背景圖片上添加一個(gè)顏色層作為覆蓋層,這個(gè)可以利用linear-gradient()函數(shù)實(shí)現(xiàn):
background-image: linear-gradient(4deg, rgba(0,255,254,0.3) 50%, rgba(0,255,254,0.3) 100%), url("https://img.php.cn/upload/article/000/000/024/612360451cede816.jpg");
此時(shí)這個(gè)還是靜態(tài)效果,怎么實(shí)現(xiàn)不斷變色的動(dòng)態(tài)效果?我們可以利用@keyframes和animation屬性來(lái)實(shí)現(xiàn)–添加動(dòng)畫(huà)效果:
-
利用animation屬性設(shè)置動(dòng)畫(huà)名稱(chēng)、播放時(shí)間、播放次數(shù)等:
body { background-image: url("https://img.php.cn/upload/article/000/000/024/612360451cede816.jpg"); background-size: cover; background-repeat: no-repeat; background-attachment: fixed; background-position: center; animation-name: background-overlay-animation; animation-duration: 5s; animation-iteration-count: infinite; animation-direction: alternate; animation-timing-function: linear; }
animation-name:指定要綁定到選擇器的關(guān)鍵幀的名稱(chēng)
animation-duration:動(dòng)畫(huà)指定需要多少秒或毫秒完成
animation-timing-function:設(shè)置動(dòng)畫(huà)將如何完成一個(gè)周期
animation-delay:設(shè)置動(dòng)畫(huà)在啟動(dòng)前的延遲間隔。
animation-iteration-count:定義動(dòng)畫(huà)的播放次數(shù)。
animation-direction:指定是否應(yīng)該輪流反向播放動(dòng)畫(huà)。
animation-fill-mode:規(guī)定當(dāng)動(dòng)畫(huà)不播放時(shí)(當(dāng)動(dòng)畫(huà)完成時(shí),或當(dāng)動(dòng)畫(huà)有一個(gè)延遲未開(kāi)始播放時(shí)),要應(yīng)用到元素的樣式。
animation-play-state:指定動(dòng)畫(huà)是否正在運(yùn)行或已暫停。
-
利用@keyframes定義每一幀動(dòng)畫(huà):
@keyframes background-overlay-animation { 0% { background-image: linear-gradient(4deg, rgba(255,78,36,0.3) 50%, rgba(255,78,36,0.3) 100%), url("https://img.php.cn/upload/article/000/000/024/612360451cede816.jpg"); } 25% { background-image: linear-gradient(4deg, rgba(213,49,127,0.3) 50%, rgba(213,49,127,0.3) 100%), url("https://img.php.cn/upload/article/000/000/024/612360451cede816.jpg"); } 50% { background-image: linear-gradient(4deg, rgba(36,182,255,0.3) 50%, rgba(36,182,255,1) 100%), url("https://img.php.cn/upload/article/000/000/024/612360451cede816.jpg"); } 100% { background-image: linear-gradient(4deg, rgba(0,255,254,0.3) 50%, rgba(0,255,254,0.3) 100%), url("https://img.php.cn/upload/article/000/000/024/612360451cede816.jpg"); } }
下面給出完整代碼:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <style> @keyframes background-overlay-animation { 0% { background-image: linear-gradient(4deg, rgba(255,78,36,0.3) 50%, rgba(255,78,36,0.3) 100%), url("https://img.php.cn/upload/article/000/000/024/612360451cede816.jpg"); } 25% { background-image: linear-gradient(4deg, rgba(213,49,127,0.3) 50%, rgba(213,49,127,0.3) 100%), url("https://img.php.cn/upload/article/000/000/024/612360451cede816.jpg"); } 50% { background-image: linear-gradient(4deg, rgba(36,182,255,0.3) 50%, rgba(36,182,255,1) 100%), url("https://img.php.cn/upload/article/000/000/024/612360451cede816.jpg"); } 100% { background-image: linear-gradient(4deg, rgba(0,255,254,0.3) 50%, rgba(0,255,254,0.3) 100%), url("https://img.php.cn/upload/article/000/000/024/612360451cede816.jpg"); } } @-webkit-keyframes background-overlay-animation { /* 兼容谷歌瀏覽器*/ 0% { background-image: linear-gradient(4deg, rgba(255,78,36,0.3) 50%, rgba(255,78,36,0.3) 100%) url("https://img.php.cn/upload/article/000/000/024/612360451cede816.jpg"); } 25% { background-image: linear-gradient(4deg, rgba(213,49,127,0.3) 50%, rgba(213,49,127,0.3) 100%), url("https://img.php.cn/upload/article/000/000/024/612360451cede816.jpg"); } 50% { background-image: linear-gradient(4deg, rgba(36,182,255,0.3) 50%, rgba(36,182,255,1) 100%), url("https://img.php.cn/upload/article/000/000/024/612360451cede816.jpg"); } 100% { background-image: linear-gradient(4deg, rgba(0,255,254,0.3) 50%, rgba(0,255,254,0.3) 100%), url("https://img.php.cn/upload/article/000/000/024/612360451cede816.jpg"); } } body { background-image: url("https://img.php.cn/upload/article/000/000/024/612360451cede816.jpg"); background-size: cover; background-repeat: no-repeat; background-attachment: fixed; background-position: center; animation-name: background-overlay-animation; animation-duration: 5s; animation-iteration-count: infinite; animation-direction: alternate; animation-timing-function: linear; } </style> </head> <body> <!-- 你的內(nèi)容放在這里 --> </body> </html>
PHP中文網(wǎng)平臺(tái)有非常多的視頻教學(xué)資源,歡迎大家學(xué)習(xí)《css視頻教程》!