今天利用CSS3來畫一個自動滾動的骰子。
思路:骰子的六個面分別用六個ul標簽,每個面的點數就是li標簽,點數的排列采用伸縮布局,然后采用定位和transform屬性將六個面翻轉折疊成立方體。
1、HTML結構:用一個類名為box的大盒子將六個面(ul)包起來,方便給整個骰子定位和添加動畫;每個ul里的li代表每個面的點數,其中第四、五、六面每一列的點數分別用一個div包起來
1 <div class="box"> 2 <ul class="one"> 3 <li></li> 4 </ul> 5 <ul class="two"> 6 <li></li> 7 <li></li> 8 </ul> 9 <ul class="three"> 10 <li></li> 11 <li></li> 12 <li></li> 13 </ul> 14 <ul class="gtThree four"> 15 <div> 16 <li></li> 17 <li></li> 18 </div> 19 <div> 20 <li></li> 21 <li></li> 22 </div> 23 24 </ul> 25 <ul class="gtThree five"> 26 <div> 27 <li></li> 28 <li></li> 29 </div> 30 <div> 31 <li></li> 32 </div> 33 <div> 34 <li></li> 35 <li></li> 36 </div> 37 </ul> 38 <ul class="gtThree six"> 39 <div> 40 <li></li> 41 <li></li> 42 <li></li> 43 </div> 44 <div> 45 <li></li> 46 <li></li> 47 <li></li> 48 </div> 49 </ul> 50 </div>
2、利用伸縮(彈性)布局畫出骰子六個面的點數
2.1給每個ul添加一個彈性布局,把每個li切成圓
1 ul { 2 width: 250px; 3 height: 250px; 4 background-color: white; 5 padding: 0; 6 list-style: none; 7 border-radius: 40px; 8 display: flex; 9 } 10 11 li { 12 width: 60px; 13 height: 60px; 14 border-radius: 50%; 15 background-color: red; 16 }
2.2 第一面:主軸居中,副軸居中
1 .one { 2 justify-content: center; 3 align-items: center; 4 }
2.2第二面:將主軸改為垂直方向,兩點均勻分開(space-around:兩點之間的空白部分是兩點前后各空白部分的兩倍),副軸居中
1 .two { 2 flex-direction: column; 3 align-items: center; 4 justify-content: space-around; 5 }
2.3第三面:將三點(水平)均勻分成三列,再分別給中間點設置副軸(垂直)居中,第三點設置位於副軸結尾
1 .three { 2 justify-content: space-around; 3 } 4 5 .three li:first-child { 6 margin-top: 10px; 7 } 8 9 .three li:nth-child(2) { 10 align-self: center; 11 } 12 13 .three li:nth-child(3) { 14 align-self: flex-end; 15 margin-bottom: 10px; 16 }
2.4第四,五,六面:每一列均分(flex:1)排列,然后每一列再分別添加一個彈性布局,並將主軸改為垂直方向,其中的點數在主軸上均勻排列,副軸居中
1 .gtThree div { 2 flex: 1; 3 display: flex; 4 flex-direction: column; 5 justify-content: space-around; 6 align-items: center; 7 }
3、利用定位和transform屬性將每個面折疊成一個立方體:給每個ul設置一個絕對定位(定位父級是div.box),這樣每個面都疊在一起,然后分別:
第一面(后面):不動;
第二面(右側面):沿Y軸旋轉90度,旋轉軸是其右邊框
第三面(左側面):沿Y軸旋轉-90度,旋轉軸是其左邊框
第四面(底面):沿X軸旋轉-90度,旋轉軸是其下邊框
第五面(頂面):沿X軸旋轉90度,旋轉軸是其上邊框
第六面(前面):沿Z軸平移250個像素
注:記得給body添加視距屬性perspective(視距就是模擬一個鏡頭到元素的距離),否則看不出立體的效果
1 body { 2 perspective: 1800px; 3 background-color: black; 4 } 5 ul { 6 position: absolute; 7 left: 0; 8 top: 0; 9 } 10 .one { 11 transform: translateZ(0); 12 } 13 14 .two { 15 transform: rotateY(90deg); 16 transform-origin: right; 17 18 } 19 20 .three { 21 transform: rotateY(-90deg); 22 transform-origin: left; 23 24 } 25 26 .four{ 27 transform: rotateX(-90deg); 28 transform-origin: bottom; 29 } 30 .five{ 31 transform: rotateX(90deg); 32 transform-origin: top; 33 } 34 .six{ 35 transform: translateZ(250px); 36 }
4、給骰子添加動畫,讓骰子動起來:定義一個change動畫,讓骰子自由旋轉,角度可以自己慢慢調
注:transform-style: preserve-3d; 開啟3D動畫,必須寫,否則沒有3D動畫效果
1 .box{ 2 transform-style: preserve-3d; 3 animation: change 30s linear infinite; 4 } 5 @keyframes change { 6 from{ 7 transform: rotateY(360deg) rotateX(720deg) rotateZ(-720deg); 8 }to{ 9 transform: rotateY(-360deg) rotateX(-720deg) rotateZ(720deg); 10 } 11 }
4.1鼠標移入事件:各個面分別展開
1 .box:hover .one{ 2 transform: translateZ(-50px); 3 } 4 .box:hover .two{ 5 left: 50px; 6 } 7 .box:hover .three{ 8 left: -50px; 9 } 10 .box:hover .four{ 11 top: 50px; 12 } 13 .box:hover .five{ 14 top: -50px; 15 } 16 .box:hover .six{ 17 transform: translateZ(300px); 18 }
★★★附上完整CSS代碼(HTML見上方)
1 body { 2 perspective: 1800px; 3 background-color: black; 4 } 5 .box{ 6 width: 250px; 7 height: 250px; 8 position: relative; 9 margin: 150px auto; 10 transform-style: preserve-3d; 11 animation: change 30s linear infinite; 12 13 } 14 .box:hover .one{ 15 transform: translateZ(-50px); 16 } 17 .box:hover .two{ 18 left: 50px; 19 } 20 .box:hover .three{ 21 left: -50px; 22 } 23 .box:hover .four{ 24 top: 50px; 25 } 26 .box:hover .five{ 27 top: -50px; 28 } 29 .box:hover .six{ 30 transform: translateZ(300px); 31 } 32 ul { 33 width: 250px; 34 height: 250px; 35 background-color: white; 36 padding: 0; 37 list-style: none; 38 border-radius: 40px; 39 display: flex; 40 position: absolute; 41 /* opacity: .9; */ 42 transition: all 1s; 43 left: 0; 44 top: 0; 45 border: 1px solid black; 46 } 47 48 li { 49 width: 60px; 50 height: 60px; 51 border-radius: 50%; 52 background-color: red; 53 } 54 55 .one { 56 justify-content: center; 57 align-items: center; 58 transform: translateZ(0); 59 } 60 61 .two { 62 flex-direction: column; 63 align-items: center; 64 justify-content: space-around; 65 66 transform: rotateY(90deg); 67 transform-origin: right; 68 69 } 70 71 .three { 72 justify-content: space-around; 73 74 transform: rotateY(-90deg); 75 transform-origin: left; 76 77 } 78 79 .three li:first-child { 80 margin-top: 10px; 81 } 82 83 .three li:nth-child(2) { 84 align-self: center; 85 } 86 87 .three li:nth-child(3) { 88 align-self: flex-end; 89 margin-bottom: 10px; 90 } 91 92 .four{ 93 transform: rotateX(-90deg); 94 transform-origin: bottom; 95 } 96 .five{ 97 transform: rotateX(90deg); 98 transform-origin: top; 99 } 100 .six{ 101 transform: translateZ(250px); 102 } 103 .gtThree div { 104 flex: 1; 105 display: flex; 106 flex-direction: column; 107 justify-content: space-around; 108 align-items: center; 109 } 110 111 @keyframes change { 112 from{ 113 transform: rotateY(360deg) rotateX(720deg) rotateZ(-720deg); 114 }to{ 115 transform: rotateY(-360deg) rotateX(-720deg) rotateZ(720deg); 116 } 117 }