最近做了一個特效,css是從網上找的,地址是這個:
把其中核心的css代碼扒出來如下:
1 /* The properties in this rule are only necessary for the 'flip' transition. 2 * We need specify the perspective to create a projection matrix. This will add 3 * some depth as the element flips. The depth number represents the distance of 4 * the viewer from the z-plane. According to the CSS3 spec, 1000 is a moderate 5 * value. 6 */ 7 .viewport-flip { 8 -webkit-perspective: 1000; 9 perspective: 1000; 10 position: absolute; 11 } 12 .flip { 13 -webkit-backface-visibility: hidden; 14 -webkit-transform: translateX(0); /* Needed to work around an iOS 3.1 bug that causes listview thumbs to disappear when -webkit-visibility:hidden is used. */ 15 backface-visibility: hidden;/*backface-visibility 屬性定義當元素不面向屏幕時是否可見*/ 16 transform: translateX(0); 17 } 18 .flip.out { 19 -webkit-transform: rotateY(-90deg) scale(.9); 20 -webkit-animation-name: flipouttoleft; 21 -webkit-animation-duration: 175ms; 22 transform: rotateY(-90deg) scale(.9); 23 animation-name: flipouttoleft; 24 animation-duration: 175ms; 25 } 26 .flip.in { 27 -webkit-animation-name: flipintoright; 28 -webkit-animation-duration: 225ms; 29 animation-name: flipintoright; 30 animation-duration: 225ms; 31 } 32 .flip.out.reverse { 33 -webkit-transform: rotateY(90deg) scale(.9); 34 -webkit-animation-name: flipouttoright; 35 transform: rotateY(90deg) scale(.9); 36 animation-name: flipouttoright; 37 } 38 .flip.in.reverse { 39 -webkit-animation-name: flipintoleft; 40 animation-name: flipintoleft; 41 } 42 @-webkit-keyframes flipouttoleft { 43 from { -webkit-transform: rotateY(0); } 44 to { -webkit-transform: rotateY(-90deg) scale(.9); } 45 } 46 @keyframes flipouttoleft { 47 from { transform: rotateY(0); } 48 to { transform: rotateY(-90deg) scale(.9); } 49 } 50 @-webkit-keyframes flipouttoright { 51 from { -webkit-transform: rotateY(0) ; } 52 to { -webkit-transform: rotateY(90deg) scale(.9); } 53 } 54 @keyframes flipouttoright { 55 from { transform: rotateY(0); } 56 to { transform: rotateY(90deg) scale(.9); } 57 } 58 @-webkit-keyframes flipintoleft { 59 from { -webkit-transform: rotateY(-90deg) scale(.9); } 60 to { -webkit-transform: rotateY(0); } 61 } 62 @keyframes flipintoleft { 63 from { transform: rotateY(-90deg) scale(.9); } 64 to { transform: rotateY(0); } 65 } 66 @-webkit-keyframes flipintoright { 67 from { -webkit-transform: rotateY(90deg) scale(.9); } 68 to { -webkit-transform: rotateY(0); } 69 } 70 @keyframes flipintoright { 71 from { transform: rotateY(90deg) scale(.9); } 72 to { transform: rotateY(0); } 73 }
做一下簡單的分析:
html結構應該如下:
1 <div id="box" class="box viewport-flip" title="點擊翻面"> 2 <a href="/" class="list flip out"><img src="http://image.zhangxinxu.com/image/blog/201210/puke-k.png" alt="紙牌正面"></a> 3 <a href="/" class="list flip"><img src="http://image.zhangxinxu.com/image/blog/201210/puke-back.png" alt="紙牌背面"></a> 4 </div>
其中viewport-flip是父容器,這里的絕對定位我沒有看明白為什么,嘗試着去掉,依舊可以正常運行,其中最關鍵的就是這個.flip.out .flip.in,在這兩個類上定義了動畫事件,以out為例子如下:
-webkit-transform: rotateY(-90deg) scale(.9);
-webkit-animation-name: flipouttoleft;
-webkit-animation-duration: 175ms;
其中規定動畫為:
flipouttoleft
1 @-webkit-keyframes flipouttoleft { 2 from { -webkit-transform: rotateY(0); } 3 to { -webkit-transform: rotateY(-90deg) scale(.9); }/*以Y軸旋轉90度,這個時候就看不見了相當於隱藏了*/ 4 }
這樣就會產生一個動畫目前正在顯示的元素以Y軸旋轉逆時針(由rotateY(-90deg)的正負控制逆時針還是順時針)90度,從開始from(旋轉0度,即不旋轉),到最終旋轉到90,旋轉過程中scale(.9)表示旋轉過程中元素大小為正常大小的90%。
同理,in則是把一個已經旋轉90度的元素相反的方向轉回0度,這樣元素就顯示了。
以上是對大神的代碼的解讀,難免有不正確的,望諒解
