在之前的文章《利用CSS3創(chuàng)建炫酷的三角背景圖像》中,我們介紹了使用CSS3創(chuàng)建炫酷的三角背景的方法。這次我們繼續(xù)CSS3效果分享,看看利用利用純CSS3如何實(shí)現(xiàn)加載動(dòng)畫效果,感興趣的可以學(xué)習(xí)了解一下~
在進(jìn)入網(wǎng)站時(shí),因?yàn)樾枰@示許多圖片,往往需要加載一段時(shí)間。如果這里添加一個(gè)動(dòng)態(tài)的加載效果,這樣就不會(huì)讓等待變得枯燥。例如下圖這樣:
本篇文章就來(lái)給大家分享兩種使用CSS3實(shí)現(xiàn)的加載動(dòng)畫效果。這兩種方法都是利用animation和@keyframes來(lái)實(shí)現(xiàn),下面我們來(lái)看看實(shí)現(xiàn)代碼:
第一種效果的實(shí)現(xiàn)方法:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <style type="text/css"> html, body { padding: 0; height: 100%; display: table; margin: 0 auto; font-size: 52px; font-family: Monaco, Consolas, "Lucida Console", monospace; background-image: url("http://subtlepatterns.subtlepatterns.netdna-cdn.com/patterns/kindajean.png"); } .loading { text-align: center; display: table-cell; vertical-align: middle; text-shadow: grey 1px 1px 1px; } .letter { float: left; width: 35px; height: 60px; position: relative; -webkit-animation: flip 2s infinite; -webkit-transform-style: preserve-3d; -webkit-transition: -webkit-transform 1s; } .letter div { width: 100%; height: 100%; position: absolute; -webkit-transform: translate(0); -webkit-backface-visibility: hidden; -webkit-animation: color 8s infinite; } .letter div.back { -webkit-transform: rotateY(180deg); } .letter:nth-child(1), .letter:nth-child(1) div { -webkit-animation-delay: 0.125s; } .letter:nth-child(2), .letter:nth-child(2) div { -webkit-animation-delay: 0.25s; } .letter:nth-child(3), .letter:nth-child(3) div { -webkit-animation-delay: 0.375s; } .letter:nth-child(4), .letter:nth-child(4) div { -webkit-animation-delay: 0.5s; } .letter:nth-child(5), .letter:nth-child(5) div { -webkit-animation-delay: 0.625s; } .letter:nth-child(6), .letter:nth-child(6) div { -webkit-animation-delay: 0.75s; } .letter:nth-child(7), .letter:nth-child(7) div { -webkit-animation-delay: 0.875s; } .letter:nth-child(8), .letter:nth-child(8) div { -webkit-animation-delay: 1s; } .letter:nth-child(9), .letter:nth-child(9) div { -webkit-animation-delay: 1.125s; } .letter:nth-child(10), .letter:nth-child(10) div { -webkit-animation-delay: 1.25s; } @-webkit-keyframes flip { 0% { -webkit-transform: rotateY(0deg) translate(0); } 40%, 100% { -webkit-transform: rotateY(180deg) translate(0); } } @-webkit-keyframes color { 0% { color: #88E488; } 25% { color: #EEADB7; } 50% { color: #90C9DB; } 75% { color: #F3B034; } 100% { color: #828282; } } </style> </head> <body> <div class="loading"> <div class="letter"> <div>L</div> <div class="back">L</div> </div> <div class="letter"> <div>o</div> <div class="back">o</div> </div> <div class="letter"> <div>a</div> <div class="back">a</div> </div> <div class="letter"> <div>d</div> <div class="back">d</div> </div> <div class="letter"> <div>i</div> <div class="back">i</div> </div> <div class="letter"> <div>n</div> <div class="back">n</div> </div> <div class="letter"> <div>g</div> <div class="back">g</div> </div> <div class="letter dot"> <div>.</div> <div class="back">.</div> </div> <div class="letter dot"> <div>.</div> <div class="back">.</div> </div> <div class="letter dot"> <div>.</div> <div class="back">.</div> </div> </div> </body> </html>
-
在上面代碼中,先使用兩個(gè)animation屬性給每個(gè)字綁定兩種動(dòng)畫flip和color,分別控制翻轉(zhuǎn)動(dòng)作和顏色變化;然后分別利用@keyframes規(guī)則,給兩個(gè)動(dòng)畫設(shè)置每一幀的動(dòng)作即可。
-
翻轉(zhuǎn)動(dòng)作需要使用transform屬性來(lái)控制,它可以向元素應(yīng)用 2D 或 3D 轉(zhuǎn)換。
效果如下:
下面的效果也是同一種實(shí)現(xiàn)思想,只是在細(xì)微處有點(diǎn)改變。
第二種效果的實(shí)現(xiàn)方法:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <style> html, body { padding: 0; height: 100%; display: table; margin: 0 auto; font-size: 52px; font-family: Monaco, Consolas, "Lucida Console", monospace; background: #F5F5F5; } .loading { top: 50%; left: 50%; width: 350px; height: 60px; font-size: 52px; position: absolute; text-align: center; margin-top: -30px; margin-left: -175px; text-shadow: #808080 1px 1px 1px; font-family: Monaco, Consolas, "Lucida Console", monospace; } .letter { float: left; width: 35px; height: 60px; position: relative; animation: flip 2s infinite; transform-style: preserve-3d; transition: transform 1s; } .letter div { color: #4B6; width: 100%; height: 100%; position: absolute; transform: translate(0); backface-visibility: hidden; animation: color 16s infinite; } .letter div.back { transform: rotateY(180deg); } .letter:nth-child(1), .letter:nth-child(1) div { animation-delay: 0.125s; } .letter:nth-child(2), .letter:nth-child(2) div { animation-delay: 0.25s; } .letter:nth-child(3), .letter:nth-child(3) div { animation-delay: 0.375s; } .letter:nth-child(4), .letter:nth-child(4) div { animation-delay: 0.5s; } .letter:nth-child(5), .letter:nth-child(5) div { animation-delay: 0.625s; } .letter:nth-child(6), .letter:nth-child(6) div { animation-delay: 0.75s; } .letter:nth-child(7), .letter:nth-child(7) div { animation-delay: 0.875s; } .letter:nth-child(8), .letter:nth-child(8) div { animation-delay: 1s; } .letter:nth-child(9), .letter:nth-child(9) div { animation-delay: 1.125s; } .letter:nth-child(10), .letter:nth-child(10) div { animation-delay: 1.25s; } @keyframes flip { 0% { transform: rotateY(0deg) translate(0); } 40%, 100% { transform: rotateY(180deg) translate(0); } } @keyframes color { 1.5% { color: #6AD; } 3%, 12.5% { color: #F80; } 14% { color: #4B6; } 15.5%, 25% { color: #F68; } 26.5% { color: #C83; } 28%, 37.5% { color: #96C; } 39% { color: #C83; } 40.5%, 50% { color: #E44; } 51.5% { color: #F80; } 53%, 62.5% { color: #4B6; } 64% { color: #F68; } 65.5%, 75% { color: #C83; } 76.5% { color: #96C; } 78%, 87.5% { color: #6AD; } 89% { color: #F80; } 90.5%, 100%, 0% { color: #4B6; } } </style> </head> <body> <div class="loader"> <div class="loading"> <div class="letter"> <div>L</div> <div class="back">L</div> </div> <div class="letter"> <div>o</div> <div class="back">o</div> </div> <div class="letter"> <div>a</div> <div class="back">a</div> </div> <div class="letter"> <div>d</div> <div class="back">d</div> </div> <div class="letter"> <div>i</div> <div class="back">i</div> </div> <div class="letter"> <div>n</div> <div class="back">n</div> </div> <div class="letter"> <div>g</div> <div class="back">g</div> </div> <div class="letter"> <div>.</div> <div class="back">.</div> </div> <div class="letter"> <div>.</div> <div class="back">.</div> </div> <div class="letter"> <div>.</div> <div class="back">.</div> </div> </div> </div> </body> </html>
效果如下:
下面介紹3個(gè)關(guān)鍵屬性animation、@keyframes和transform:
-
CSS3
animation
(動(dòng)畫) 屬性
語(yǔ)法:animation: name duration timing-function delay iteration-count direction fill-mode play-state;
animation-name:指定要綁定到選擇器的關(guān)鍵幀的名稱 animation-duration:動(dòng)畫指定需要多少秒或毫秒完成 animation-timing-function:設(shè)置動(dòng)畫將如何完成一個(gè)周期 animation-delay:設(shè)置動(dòng)畫在啟動(dòng)前的延遲間隔。 animation-iteration-count:定義動(dòng)畫的播放次數(shù)。 animation-direction:指定是否應(yīng)該輪流反向播放動(dòng)畫。 animation-fill-mode:規(guī)定當(dāng)動(dòng)畫不播放時(shí)(當(dāng)動(dòng)畫完成時(shí),或當(dāng)動(dòng)畫有一個(gè)延遲未開始播放時(shí)),要應(yīng)用到元素的樣式。 animation-play-state:指定動(dòng)畫是否正在運(yùn)行或已暫停。
-
@keyframes
規(guī)則
使用@keyframes規(guī)則可以創(chuàng)建動(dòng)畫。創(chuàng)建動(dòng)畫是通過(guò)逐步改變從一個(gè)CSS樣式設(shè)定到另一個(gè)。簡(jiǎn)單來(lái)說(shuō):@keyframes就是用來(lái)設(shè)置動(dòng)畫每一幀動(dòng)作的。
@keyframes需要和animation 屬性一起使用才能實(shí)現(xiàn)動(dòng)畫:
@keyframe規(guī)則由關(guān)鍵字“@keyframe”組成,后面接著是給出動(dòng)畫名稱的標(biāo)識(shí)符(將使用animation-name引用),隨后是通過(guò)一組樣式規(guī)則(用大括號(hào)分隔)。然后,通過(guò)使用標(biāo)識(shí)符作為animation-name屬性的值,將動(dòng)畫應(yīng)用于元素。例如:
/* 定義動(dòng)畫*/ @keyframes 動(dòng)畫名稱{ /* 樣式規(guī)則*/ } /* 將它應(yīng)用于元素 */ .element { animation-name: 動(dòng)畫名稱(在@keyframes中已經(jīng)聲明好的); /* 或使用動(dòng)畫簡(jiǎn)寫屬性*/ animation: 動(dòng)畫名稱 1s ... }
-
CSS3
transform
屬性
transform 屬性向元素應(yīng)用 2D 或 3D 轉(zhuǎn)換。該屬性允許我們對(duì)元素進(jìn)行旋轉(zhuǎn)、縮放、移動(dòng)或傾斜。
語(yǔ)法:transform: none|transform-functions;
none 定義不進(jìn)行轉(zhuǎn)換。 matrix(n,n,n,n,n,n) 定義 2D 轉(zhuǎn)換,使用六個(gè)值的矩陣。 matrix3d(n,n,n,n,n,n,n,n,n,n,n,n,n,n,n,n) 定義 3D 轉(zhuǎn)換,使用 16 個(gè)值的 4x4 矩陣。 translate(x,y) 定義 2D 轉(zhuǎn)換。 translate3d(x,y,z) 定義 3D 轉(zhuǎn)換。 translateX(x) 定義轉(zhuǎn)換,只是用 X 軸的值。 translateY(y) 定義轉(zhuǎn)換,只是用 Y 軸的值。 translateZ(z) 定義 3D 轉(zhuǎn)換,只是用 Z 軸的值。 scale(x,y) 定義 2D 縮放轉(zhuǎn)換。 scale3d(x,y,z) 定義 3D 縮放轉(zhuǎn)換。 scaleX(x) 通過(guò)設(shè)置 X 軸的值來(lái)定義縮放轉(zhuǎn)換。 scaleY(y) 通過(guò)設(shè)置 Y 軸的值來(lái)定義縮放轉(zhuǎn)換。 scaleZ(z) 通過(guò)設(shè)置 Z 軸的值來(lái)定義 3D 縮放轉(zhuǎn)換。 rotate(angle) 定義 2D 旋轉(zhuǎn),在參數(shù)中規(guī)定角度。 rotate3d(x,y,z,angle) 定義 3D 旋轉(zhuǎn)。 rotateX(angle) 定義沿著 X 軸的 3D 旋轉(zhuǎn)。 rotateY(angle) 定義沿著 Y 軸的 3D 旋轉(zhuǎn)。 rotateZ(angle) 定義沿著 Z 軸的 3D 旋轉(zhuǎn)。 skew(x-angle,y-angle) 定義沿著 X 和 Y 軸的 2D 傾斜轉(zhuǎn)換。 skewX(angle) 定義沿著 X 軸的 2D 傾斜轉(zhuǎn)換。 skewY(angle) 定義沿著 Y 軸的 2D 傾斜轉(zhuǎn)換。 perspective(n) 為 3D 轉(zhuǎn)換元素定義透視視圖。
PHP中文網(wǎng)平臺(tái)有非常多的視頻教學(xué)資源,歡迎大家學(xué)習(xí)《css視頻教程》!