1. 程式人生 > >微信小程式—Flex佈局

微信小程式—Flex佈局

參考教程:http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html

        https://xluos.github.io/demo/flexbox/

語法:

一、Flex 佈局是什麼?

Flex 是 Flexible Box 的縮寫,意為"彈性佈局",用來為盒狀模型提供最大的靈活性。

任何一個容器都可以指定為 Flex 佈局。


.box{
  display: flex;
}

行內元素也可以使用 Flex 佈局。


.box{
  display: inline-flex;
}

Webkit 核心的瀏覽器 (蘋果系統),必須加上-webkit

字首。


.box{
  display: -webkit-flex; /* Safari */
  display: flex;
}

注意,設為 Flex 佈局以後,子元素的floatclearvertical-align屬性將失效。

二、基本概念

採用 Flex 佈局的元素,稱為 Flex 容器(flex container),簡稱"容器"。它的所有子元素自動成為容器成員,稱為 Flex 專案(flex item),簡稱"專案"。

容器預設存在兩根軸:水平的主軸(main axis)和垂直的交叉軸(cross axis)。主軸的開始位置(與邊框的交叉點)叫做main start,結束位置叫做main end

;交叉軸的開始位置叫做cross start,結束位置叫做cross end

專案預設沿主軸排列。單個專案佔據的主軸空間叫做main size,佔據的交叉軸空間叫做cross size

三、容器的屬性

以下6個屬性設定在容器上。

  • flex-direction
  • flex-wrap
  • flex-flow
  • justify-content
  • align-items
  • align-content

3.1 flex-direction屬性

  flex-direction屬性決定主軸的方向(即專案的排列方向)。


.box {
  flex-direction: row | row-reverse | column | column-reverse;
}

  它可能有4個值。

  • row(預設值):主軸為水平方向,起點在左端。
  • row-reverse:主軸為水平方向,起點在右端。
  • column:主軸為垂直方向,起點在上沿。
  • column-reverse:主軸為垂直方向,起點在下沿。

3.2 flex-wrap屬性

  預設情況下,專案都排在一條線(又稱"軸線")上。flex-wrap屬性定義,如果一條軸線排不下,如何換行。


.box{
  flex-wrap: nowrap | wrap | wrap-reverse;
}

  它可能取三個值。

(1)nowrap(預設):不換行(多的則自動調整)

(2)wrap:換行,第一行在上方。

(3)wrap-reverse:換行,第一行在下方。

3.3 flex-flow

flex-flow屬性是flex-direction屬性和flex-wrap屬性的簡寫形式,預設值為row nowrap


.box {
  flex-flow: <flex-direction> || <flex-wrap>;
}

3.4 justify-content屬性

justify-content屬性定義了專案在主軸上的對齊方式。


.box {
  justify-content: flex-start | flex-end | center | space-between | space-around;
}

它可能取5個值,具體對齊方式與軸的方向有關。下面假設主軸為從左到右。

  • flex-start(預設值):左對齊
  • flex-end:右對齊
  • center: 居中
  • space-between:兩端對齊,專案之間的間隔都相等。
  • space-around:每個專案兩側的間隔相等。所以,專案之間的間隔比專案與邊框的間隔大一倍。

3.5 align-items屬性

align-items屬性定義專案在交叉軸上如何對齊。


.box {
  align-items: flex-start | flex-end | center | baseline | stretch;
}

它可能取5個值。具體的對齊方式與交叉軸的方向有關,下面假設交叉軸從上到下。

  • flex-start:交叉軸的起點對齊。
  • flex-end:交叉軸的終點對齊。
  • center:交叉軸的中點對齊。
  • baseline: 專案的第一行文字的基線對齊。
  • stretch(預設值):如果專案未設定高度或設為auto,將佔滿整個容器的高度。

3.6 align-content屬性

align-content屬性定義了多根軸線的對齊方式。如果專案只有一根軸線,該屬性不起作用。


.box {
  align-content: flex-start | flex-end | center | space-between | space-around | stretch;
}

該屬性可能取6個值。

  • flex-start:與交叉軸的起點對齊。
  • flex-end:與交叉軸的終點對齊。
  • center:與交叉軸的中點對齊。
  • space-between:與交叉軸兩端對齊,軸線之間的間隔平均分佈。
  • space-around:每根軸線兩側的間隔都相等。所以,軸線之間的間隔比軸線與邊框的間隔大一倍。
  • stretch(預設值):軸線佔滿整個交叉軸。

四、專案的屬性

以下6個屬性設定在專案上。

  • order
  • flex-grow
  • flex-shrink
  • flex-basis
  • flex
  • align-self

4.1 order屬性

order屬性定義專案的排列順序。數值越小,排列越靠前,預設為0。


.item {
  order: <integer>;
}

4.2 flex-grow屬性

flex-grow屬性定義專案的放大比例,預設為0,即如果存在剩餘空間,也不放大。


.item {
  flex-grow: <number>; /* default 0 */
}

如果所有專案的flex-grow屬性都為1,則它們將等分剩餘空間(如果有的話)。如果一個專案的flex-grow屬性為2,其他專案都為1,則前者佔據的剩餘空間將比其他項多一倍。

4.3 flex-shrink屬性

flex-shrink屬性定義了專案的縮小比例,預設為1,即如果空間不足,該專案將縮小。


.item {
  flex-shrink: <number>; /* default 1 */
}

如果所有專案的flex-shrink屬性都為1,當空間不足時,都將等比例縮小。如果一個專案的flex-shrink屬性為0,其他專案都為1,則空間不足時,前者不縮小。

負值對該屬性無效。

4.4 flex-basis屬性

flex-basis屬性定義了在分配多餘空間之前,專案佔據的主軸空間(main size)。瀏覽器根據這個屬性,計算主軸是否有多餘空間。它的預設值為auto,即專案的本來大小。


.item {
  flex-basis: <length> | auto; /* default auto */
}

它可以設為跟widthheight屬性一樣的值(比如350px),則專案將佔據固定空間。

4.5 flex屬性

flex屬性是flex-growflex-shrink 和 flex-basis的簡寫,預設值為0 1 auto。後兩個屬性可選。


.item {
  flex: none | [ <'flex-grow'> <'flex-shrink'>? || <'flex-basis'> ]
}

該屬性有兩個快捷值:auto (1 1 auto) 和 none (0 0 auto)。

建議優先使用這個屬性,而不是單獨寫三個分離的屬性,因為瀏覽器會推算相關值。

4.6 align-self屬性

align-self屬性允許單個專案有與其他專案不一樣的對齊方式,可覆蓋align-items屬性。預設值為auto,表示繼承父元素的align-items屬性,如果沒有父元素,則等同於stretch


.item {
  align-self: auto | flex-start | flex-end | center | baseline | stretch;
}

該屬性可能取6個值,除了auto,其他都與align-items屬性完全一致。

例項:

  1、不用flex佈局

 1 <!--pages/index/index.wxml-->
 2 <view class="container">
 3 
 4   <view class="txt01">
 5     <text >pages</text>
 6   </view>
 7 
 8   <view class="txt02">
 9     <text >pages</text>
10   </view>
11 
12   <view class="txt03">
13     <text >pages</text>
14   </view>
15 
16   <view class="txt04">
17     <text >pages</text>
18   </view>  
19   
20 </view>
21 /* pages/index/index.wxss */
22 .txt01{
23   width: 150rpx;
24   height: 150rpx;
25   background-color: red;
26 }
27 .txt02{
28   width: 150rpx;
29   height: 150rpx;
30   background-color: green;
31 }
32 .txt03{
33   width: 150rpx;
34   height: 150rpx;
35   background-color: blue;
36 }
37 .txt04{
38   width: 150rpx;
39   height: 150rpx;
40   background-color: yellow;
41 }

  2、加上flex-direction

1 /* pages/index/index.wxss */
2 .container{
3   display: flex;
4   flex-direction: row;   // 預設row
5 }
6 。。。。。。。。
1 /* pages/index/index.wxss */
2 .container{
3   display: flex;
4   flex-direction: column;  //
5 }
1 /* pages/index/index.wxss */
2 .container{
3   display: flex;
4   flex-direction: row-reverse;
5 }
 
1 /* pages/index/index.wxss */
2 .container{
3   display: flex;
4   flex-direction: column-reverse;
5 }

   3、加上flex-wrap

1 /* pages/index/index.wxss */
2 .container{
3   display: flex;
4   flex-wrap: nowrap //預設,不換行 自動調整
5 }
6 。。。。。。。。。。
1 /* pages/index/index.wxss */
2 .container{
3   display: flex;
4   flex-direction: column;  //縱向
5   flex-wrap: nowrap;
6 }
1 /* pages/index/index.wxss */
2 .container{
3   display: flex;
4   flex-wrap: wrap;  //換行
5 }
6 。。。。。
1 /* pages/index/index.wxss */
2 .container{
3   display: flex;
4   flex-direction: column;
5   flex-wrap: wrap;
6 }
1 /* pages/index/index.wxss */
2 .container{
3   display: flex;
4   flex-wrap: wrap-reverse; //換行 逆
5 }
1 /* pages/index/index.wxss */
2 .container{
3   display: flex;
4   flex-direction: column;
5   flex-wrap: wrap-reverse;
6 }

  3、加上flex-flow

1 /* pages/index/index.wxss */
2 .container{
3   display: flex;
4   flex-flow: row nowrap  // 相當於:flex-direction:row; flex-wrap:nowrap
5 }
1 /* pages/index/index.wxss */
2 .container{
3   display: flex;
4   flex-flow: row wrap   //相當於  flex-direction:row; flex-wrap:wrap
5 }
1 /* pages/index/index.wxss */
2 .container{
3   display: flex;
4   flex-flow: row wrap-reverse  // 相當於flex-direction:row;flex-wrap:wrap-reverse
5 }
 1 /* pages/index/index.wxss */
 2 .container{
 3   display: flex;
 4   flex-flow: column nowrap    //相當於:flex-direction:column; flex-wrap:nowrap
 5 }
 6 。。。。。。。
 7 /* pages/index/index.wxss */
 8 .container{
 9   display: flex;
10   flex-flow: column wrap    //相當於:flex-direction:column; flex-wrap:wrap
11 }
12 。。。。。。。
13 /* pages/index/index.wxss */
14 .container{
15   display: flex;
16   flex-flow: column wrap-rever  //相當於:flex-direction:column;flex-wrap:wrap-reverse
17 }

 4、加上justify-content

1 /* pages/index/index.wxss */
2 .container{
3   display: flex;
4   flex-direction: row;
5   justify-content: flex-start  //左對齊,無間隙
6 }
1 /* pages/index/index.wxss */
2 .container{
3   display: flex;
4   flex-direction: row;
5   justify-content: flex-end  //右對齊,無間隙
6 } 
1 /* pages/index/index.wxss */
2 .container{
3   display: flex;
4   flex-direction: row;
5   justify-content: center   //居中,無間隙
6 }
1 /* pages/index/index.wxss */
2 .container{
3   display: flex;
4   flex-direction: row;
5   justify-content: space-between  //兩端對齊,專案之間的間隔都相等
6 }
1 /* pages/index/index.wxss */
2 .container{
3   display: flex;
4   flex-direction: row;
5   justify-content: space-around  //每個專案兩側的間隔相等。所以,專案之間的間隔比專案與邊框的間隔大一倍
6 }

 

 

 

&n