CSS 實現加載動畫之五-光盤旋轉


今天做的這個動畫叫光盤旋轉,名字自己取的。動畫的效果估計很多人都很熟悉,就是微信朋友圈里的加載動畫。做過前面幾個動畫,發現其實都一個原理,就是如何將動畫的元素如何分離出來。這個動畫的實現也很簡單,關鍵點在於如何將元素拼湊成風車形狀。然后定義動畫的關鍵幀為rotate 360度,應用於整個元素上,就可以使整個元素旋轉起來。案例在請在chrome中查看。

 

1. 先看截圖

 

2. 代碼實現思路

2.1 首先還是定義四個元素,元素的中心為這四個元素組成的圓的圓心。這樣定義的目的可以保證元素拼湊成一個正圓。

2.2 在單個元素的頭尾各定義一個子元素,子元素旋轉一定的角度,使其平行排列,中間剛好留一個正圓的位置。這里用了rotate和translate屬性,沒有用skew屬性,是因為skew方法會使元素拉伸后變小。

2.3 將每個元素的子元素都定義不同的背景色,定義完成后,會形成8個不同的顏色排列,此時元素的形狀已經產生了。需要注意的是,最后一個元素需要裁剪下,因為有可能會覆蓋第一個元素。案例中第4,8個子元素是分開寫的。

2.4 此時在圓心位置定義一個圓,圓的大小剛好覆蓋中間的空隙位置。外層容器也設為一個圓,大小為能全部顯示所有的背景顏色,多余的部分截斷隱藏。

2.5 定義動畫,並在外層容器上應用這個動畫。這個動畫的方式比較簡單,就是旋轉,直接使用rotate即可。

 

3. 源代碼

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <meta http-equiv="Content-Type" Content="text/html; charset=utf-8;">
 5 <title>CSS3實現加載的動畫效果5</title>
 6 <meta name="author" content="rainna" />
 7 <meta name="keywords" content="rainna's css lib" />
 8 <meta name="description" content="CSS3" />
 9 <style>
10 *{margin:0;padding:0;}
11 body{background:#464646;}
12 
13 .m-load{width:490px;height:330px;margin:100px auto;background:url('load.png') center center no-repeat;}
14 
15 /** 加載動畫的靜態樣式 **/
16 .m-load2{position:relative;width:52px;height:52px;margin:0 auto;border-radius:52px;border:2px solid #fff;overflow:hidden;}
17 .m-load2 .item{position:absolute;left:50%;top:0;width:20px;height:100%;margin-left:-10px;}
18 .m-load2 .item:nth-child(2){-webkit-transform:rotate(45deg);}
19 .m-load2 .item:nth-child(3){-webkit-transform:rotate(90deg);}
20 .m-load2 .item:nth-child(4){-webkit-transform:rotate(135deg);clip:rect(-26px,18px,26px,-18px);}
21 .m-load2 .item:nth-child(5){-webkit-transform:rotate(135deg);clip:rect(26px,37px,78px,2px);}
22 .m-load2 .item:before,.m-load2 .item:after{content:'';position:absolute;left:0;width:18px;height:100%;}
23 .m-load2 .item:before{bottom:52%;border-left:2px solid #fff;-webkit-transform-origin:left bottom;-webkit-transform:translate(100%,0) rotate(-45deg);}
24 .m-load2 .item:after{top:52%;border-right:2px solid #fff;-webkit-transform-origin:right top;-webkit-transform:translate(-100%,0) rotate(-45deg);}
25 .m-load2 .item:nth-child(1):before{background:#48ec53;}
26 .m-load2 .item:nth-child(1):after{background:#f75e5a;}
27 .m-load2 .item:nth-child(2):before{background:#a6ea4b;}
28 .m-load2 .item:nth-child(2):after{background:#724dee;}
29 .m-load2 .item:nth-child(3):before{background:#e8d84b;}
30 .m-load2 .item:nth-child(3):after{background:#44abec;}
31 .m-load2 .item:nth-child(4):before{background:#fdc103;}
32 .m-load2 .item:nth-child(5):after{background:#51ddeb;}
33 
34 .m-load2 .point{position:absolute;left:50%;top:50%;width:18px;height:18px;margin:-9px 0 0 -9px;border-radius:18px;background:#464646;}
35 
36 /** 加載動畫 **/
37 @-webkit-keyframes load{
38     100%{-webkit-transform:rotate(360deg);}
39 }
40 .m-load2{-webkit-animation:load 1.8s linear infinite;}
41 </style>
42 </head>
43 
44 <body>
45 <div class="m-load"></div>
46 
47 <div class="m-load2">
48     <div class="item"></div>
49     <div class="item"></div>
50     <div class="item"></div>
51     <div class="item"></div>
52     <div class="item"></div>
53     <div class="point"></div>
54 </div>
55 </body>
56 </html>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM