首頁  >  文章  >  web前端  >  CSS3的Flexbox骰子佈局的實現與問題解說

CSS3的Flexbox骰子佈局的實現與問題解說

高洛峰
高洛峰原創
2017-02-16 13:44:481268瀏覽

本文中只是熟悉基本屬性的用法,並完成一組骰子各個面的製作。在下面的內容我不會涉及flexbox一些比較棘手的問題,像是舊版語法、供應商前綴、瀏覽器怪癖等:

一、First Face
我們知道,骰子有六個面,每個面的點的個數代表該面的值,第一個面由一個水平垂直居中的點組成。以下來看具體的實作:

<section name="01" class="face-01">   
  <span class="dot"></span>   
 </section>   
face-01 {   
display: flex;   
justify-content: center;   
align-items: center;

關於justify-content和align-items的用法請參考這裡justify-content,align-items。使用flexbox,垂直居中兩行屬性就可以搞定,很easy!

二、Second Face

.face-02 {   
 display: flex;   
 justify-content: space-between;   
}   
.face-02 .dot:nth-of-type(2) {   
 align-self: flex-end;   
}   
  <section name="02" class="face-02">   
   <span class="dot"></span>   
   <span class="dot"></span>   
  </section>

這裡我們不能使用align-items屬性,使用它兩個點都會受影響,flexbox提供了一個align-self屬性,這個屬性可以讓我們更方便的控制flex items的各項沿著cross axias方向,設定不同的佈局。 align-self的用法參考這裡align-self。

 

三、Third Face

.face-03 {   
 display: flex;   
 justify-content: space-between;   
}   
.face-03 .dot:nth-of-type(2) {   
 align-self: center;   
}   
.face-03 .dot:nth-of-type(3) {   
 align-self: flex-end;   
}   
<section name="03" class="face-03">   
 <span class="dot"></span>   
 <span class="dot"></span>   
 <span class="dot"></span>   
</section>

該face與second face 所使用的屬性相同,不再解釋。

 

四、Fourth Face

.face-04 {   
 display: flex;   
 justify-content: space-between;   
 flex-direction: column;   
}   
.face-04 .column {   
 display: flex;   
 justify-content: space-between;   
}   
<section name="04" class="face-04">   
  <div class="column">   
    <span class="dot"></span>   
    <span class="dot"></span>   
  </div>   
  <div class="column">   
    <span class="dot"></span>   
    <span class="dot"></span>   
  </div>   
</section>

本例中使用了flex-direction,從字面意思可以看出,是用來控制flex的方向,即按列還是按行來佈局,該屬性更詳細的用法可以參考這裡flex-direction

後面Fifth Face 和Sixth Face,根據前面的版面思想,就很easy了不再贅述!

寫到此,想想配合JS寫一個玩骰子的小遊戲應該很easy了吧。

 

五、實現1,2,3,4,6,12等份

.row {   
  display: flex;   
  box-sizing: border-box;   
}   
  
.column {   
  margin: 10px;   
  flex-grow: 1;   
  flex-shrink: 1;   
  flex-basis: 0;   
  box-sizing: border-box;   
}   
<section class="row">   
  <div class="column">One</div>   
</section>   
<section class="row">   
  <div class="column">One Half</div>   
  <div class="column">One Half</div>   
</section>   
<section class="row">   
  <div class="column">One Third</div>   
  <div class="column">One Third</div>   
  <div class="column">One Third</div>   
</section>   
<section class="row">   
  <div class="column">One Fourth</div>   
  <div class="column">One Fourth</div>   
  <div class="column">One Fourth</div>   
  <div class="column">One Fourth</div>   
</section>   
<section class="row">   
  <div class="column">One Sixth</div>   
  <div class="column">One Sixth</div>   
  <div class="column">One Sixth</div>   
  <div class="column">One Sixth</div>   
  <div class="column">One Sixth</div>   
  <div class="column">One Sixth</div>   
</section>   
<section class="row">   
  <div class="column">One Twelve</div>   
  <div class="column">One Twelve</div>   
  <div class="column">One Twelve</div>   
  <div class="column">One Twelve</div>   
  <div class="column">One Twelve</div>   
  <div class="column">One Twelve</div>   
  <div class="column">One Twelve</div>   
  <div class="column">One Twelve</div>   
  <div class="column">One Twelve</div>   
  <div class="column">One Twelve</div>   
  <div class="column">One Twelve</div>   
  <div class="column">One Twelve</div>   
</section>

在本例中使用了flex-grow,flex-shrink,flex-basis三個屬性。
1. flex-grow:根據需要用來定義伸縮項目的擴展能力。它接受一個不帶單位的值做為一個比例。主要用來決定伸縮容器剩餘空間按比例應擴展多少空間。
如果所有伸縮項目的“flex-grow”都設定了“1”,那麼每個伸縮項目將設定為一個大小相等的剩餘空間。如果你為其中一個伸縮項目設定了“flex-grow”值為“2”,那麼這個伸縮項目所佔的剩餘空間是其他伸縮項目所佔剩餘空間的兩倍。負值無效。
2. flex-shrink:根據需要用來定義伸縮項目收縮的能力。負值同樣無效。
3. flex-basis: 用來設定伸縮基準值,剩餘的空間以比率伸縮,不支持負值。如果設定為0,圍繞內容的額外的空間不會考慮。如果設定為auto,額外的空間是基於flex-grow的值分配。

六、實現2-3-7佈局

.row237 .column:first-of-type {   
  flex-grow: 2;   
  flex-basis: 5px;   
}   
.row237 .column:nth-of-type(2) {   
  flex-grow: 3;   
  flex-basis: 18px;   
}   
.row237 .column:nth-of-type(3) {   
  flex-grow: 7;   
  flex-basis: 70.5px;   
}   
  
<section class="row row237">   
  <div class="column">One Half</div>   
  <div class="column">One Third</div>   
  <div class="column">One Seventh</div>   
</section>

此處各項flex-basis的值的計算,應該有個公式(待解決),如果有這個公式,配合sass,less等預處理語言實現多列自適應佈局將會很方便。

更多CSS3的Flexbox骰子佈局的實現及問題講解相關文章請關注PHP中文網!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn