CSS3 Gradient 分爲 linear-gradient(線性漸變)和 radial-gradient(徑向漸變)。而咱們今天主要是經過一個小例子來針對線性漸變來剖析其具體的用法。web
今天的例子就是用CSS3 的線性漸變製做一個大體的針孔注射器。函數
首先來看一下最終效果,例子比較簡單,主要應用到的就是CSS3裏的 linear-gradient
。學習
http://jsbin.com/vecujedubu/edit?outputspa
看完Demo,咱們首先來看一下HTML結構。code
<!-- 注射器父容器 --> <div class="injector_wrap"> <!-- 注射器的針管 --> <div class="injector__box"> <!-- 注射器橡膠塞 --> <div class="injector__hat"> <!-- 注射器的推柄 --> <div class="injector__handle"> </div> <!-- 注射器的推柄 --> </div> <!-- 注射器橡膠塞 --> </div> <!-- 注射器的針管 --> </div> <!-- 注射器父容器 -->
CSS 部分開發
.injector_wrap{ width: 200px; margin: 120px auto 0; } .injector__box{ background: -webkit-linear-gradient(45deg ,#ccc,#ecf0f1,#CCC); background : -webkit-gradient(linear,left center,right center, from(#ccc) , color-stop(0.5,#ecf0f1) , to(#CCC) ); width: 40px; height: 180px; position: relative; } .injector__box::before{ position: absolute; content: " "; left: 15px; width: 10px; height: 30px; background: -webkit-linear-gradient(left ,#ccc,#ecf0f1,#CCC); background : -webkit-gradient(linear,left center,right center,from(#ccc),color-stop(0.5,#ecf0f1),to(#ccc)); bottom: 100%; } .water{ width: 100%; position: absolute; top: 0; bottom: 20px; background-color: #62d2e2; } .water::before{ position: absolute; content: " "; width: 10px; background-color: #62d2e2; top: -30px; height: 30px; left: 15px; } .injector__hat{ transition: all ease 0.6s ; position: absolute; width: 100%; height: 20px; background: -webkit-linear-gradient(left ,#282E33,#5D5D5D,#282E33); background:-webkit-gradient(linear,left center,right center,from(#282e33), color-stop(0.5,#5d5d5d), to(#282e33)); bottom: 0px; -webkit-animation: autoPushWater 3s linear 0s infinite; -o-animation: autoPushWater 3s linear 0s infinite; animation: autoPushWater 3s linear 0s infinite; } .injector__hat::before{ position: absolute; content: " "; width: 0; height: 0; border-left : 20px dashed transparent; border-right : 20px dashed transparent; border-bottom : 15px solid #2c3e50; bottom: 100%; } .injector__handle{ position: absolute; content: " "; left: 10px; width: 20px; height: 180px; background-color: #c0392b; background:-webkit-gradient(linear,left center,right center, from(#80261c),color-stop(0.5,#c0392b), to(#420600)); background: -webkit-linear-gradient(left ,#80261C,#c0392b,#420600); top: 100%; } .injector__handle::after{ position: absolute; content: " "; left: -20px; width: 60px; height: 4px; background:-webkit-gradient(linear,left center,right center,from(#80261c), color-stop(0.5,#c0392b), to(#420600)); background: -webkit-linear-gradient(left ,#80261C,#c0392b,#420600); top: 100%; } @-webkit-keyframes autoPushWater { from {bottom: 0 } 50%{bottom:144px;} to {bottom: 0px; } } @-o-keyframes autoPushWater { from {bottom: 0 } 50%{bottom:144px;} to {bottom: 0px; } } @-moz-keyframes autoPushWater { from {bottom: 0 } 50%{bottom:144px;} to {bottom: 0px; } } @keyframes autoPushWater { from {bottom: 0 } 50%{bottom:144px;} to {bottom: 0px; } }
CSS部分其實也蠻簡單的,其餘部分不說了,相信均可以看懂是什麼意思、有什麼用。咱們就把此次的核心內容linear-gradient
拿出來單獨分析一下。get
細心的小夥伴應該會發現,在CSS中linear-gradient
出現了兩種方式,都是webkit
前綴的。其實二者都是Webkit內核對於linear-gradient
的實現,只不過一個是早期實現-webkit-gradient
,一個是修改後的參照標準的實現-webkit-linear-gradient
。animation
首先咱們來分析一下早期實現-webkit-gradient
.it
-webkit-gradient(linear,left center,right center,from(#80261c), color-stop(0.5,#c0392b), to(#420600)); // 原始語法 -webkit-gradient(<type>, <point> [, <radius>]?, <point> [, <radius>]? [, <stop>]*) //老式語法書寫規則
-webkit-gradient 是 webkit
引擎對漸變的實現參數,一共有五個。io
第一個參數表示漸變類型(type),能夠是 linear
(線性漸變)或者 radial
(徑向漸變)。
第二個參數和第三個參數,都是一對值,分別表示漸變起點和終點。這對值能夠用座標形式表示,也能夠用關鍵值表示,好比 left top(左上角)和left bottom(左下角)。
第四個和第五個參數,分別是兩個color-stop函數。color-stop 函數接受兩個參數,第一個表示漸變的位置,0爲起點,0.5爲中點,1爲結束點;第二個表示該點的顏色。
若是color-stop
第一個參數是0或者1的話,那麼可使用from或者to來代替,from、to 只有1個參數——色值。from(hex)
效果上等於 color-stop(0,hex)
,而 to(hex)
等同於 color-stop(1,hex)
。
因此上面例子中的代碼能夠修改成。
-webkit-gradient(linear,left center,right center,color-stop(0,#80261c), color-stop(0.5,#c0392b), color-stop(1,#420600)); -webkit-gradient(linear,left center,right center,from(#80261c), color-stop(0.5,#c0392b), to(#420600));
在WebKit的-webkit-linear-gradient()中不能同時設置位置和角度。能夠經過設置顏色中間點達到相同的效果。即若是設置了 left top 和 漸變軸角度的話會致使CSS解析錯誤,沒法達到預期效果。
接下來是Webkit按照標準實現的漸變-webkit-linear-gradient
。
-webkit-linear-gradient(left,#80261C,#c0392b,#420600); // 原始語法 -webkit-linear-gradient([<point> || <angle>,]? <stop>, <stop> [, <stop>]);
-webkit-linear-gradient 有三個參數。
第一個參數表示線性漸變的方向,top 是從上到下、left 是從左到右,若是定義成 left top,那就是從左上角到右下角。
第二個和第三個參數分別是起點顏色和終點顏色。你還能夠在它們之間插入更多的參數,表示多種顏色的漸變。
在標準實現中,取消了from、to、color-stop等函數,開發人員只需定義漸變起點或者漸變軸角度以及漸變的色值便可實現漸變。
注意:漸變起點和漸變軸角度不可同時設置,這樣會引發CSS解析錯誤,沒法達到預期效果。
慼慼然不知所言,到這裏算是收尾了。本篇算是個徹徹底底的標題黨,並無去分析FF、Opera以及IE下的CSS漸變實現。截至目前爲止,FF、Opera的實現和Webkit的新版實現基本保持一致,惟一的問題則是IE,歷史版本和新版本的IE各有其實現,有興趣的話能夠去查找相關資料學習參考一下。
參考資料: