布局 Layout
# 栅格布局
# 基本用法 span
<template>
<mc-container>
<mc-row class="row">
<mc-col :span="24" class="cont">span=24</mc-col>
</mc-row>
<mc-row class="row">
<mc-col :span="12" class="cont"> span=12</mc-col>
<mc-col :span="12" class="cont">span=12</mc-col>
</mc-row>
<mc-row class="row">
<mc-col :span="8" class="cont">span=8</mc-col>
<mc-col :span="8" class="cont">span=8</mc-col>
<mc-col :span="8" class="cont">span=8</mc-col>
</mc-row>
<mc-row class="row">
<mc-col :span="6" class="cont">span=6</mc-col>
<mc-col :span="6" class="cont">span=6</mc-col>
<mc-col :span="6" class="cont">span=6</mc-col>
<mc-col :span="6" class="cont">span=6</mc-col>
</mc-row>
<mc-row class="row">
<mc-col :span="4" class="cont">span=4</mc-col>
<mc-col :span="4" class="cont">span=4</mc-col>
<mc-col :span="4" class="cont">span=4</mc-col>
<mc-col :span="4" class="cont">span=4</mc-col>
<mc-col :span="4" class="cont">span=4</mc-col>
<mc-col :span="4" class="cont">span=4</mc-col>
</mc-row>
<mc-row class="row">
<mc-col :span="3" class="cont">span=3</mc-col>
<mc-col :span="3" class="cont">span=3</mc-col>
<mc-col :span="3" class="cont">span=3</mc-col>
<mc-col :span="3" class="cont">span=3</mc-col>
<mc-col :span="3" class="cont">span=3</mc-col>
<mc-col :span="3" class="cont">span=3</mc-col>
<mc-col :span="3" class="cont">span=3</mc-col>
<mc-col :span="3" class="cont">span=3</mc-col>
</mc-row>
</mc-container>
</template>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# offset 间隔
<template>
<mc-container>
<mc-row class="row">
<mc-col :span="4" :offset="3" class="cont">span=4offset=3</mc-col>
<mc-col :span="4" :offset="3" class="cont">span=4offset=3</mc-col>
<mc-col :span="4" :offset="2" class="cont">span=4offset=2</mc-col>
</mc-row>
</mc-container>
</template>
2
3
4
5
6
7
8
9
# flex 布局
# mc-flex 的容器属性
# 1、direction
direction 属性决定主轴的方向(即项目的排列方向)
direction有4个值。
· row(默认值):主轴为水平方向,起点在左端。
· row-reverse:主轴为水平方向,起点在右端。
· column:主轴为垂直方向,起点在上沿。
· column-reverse:主轴为垂直方向,起点在下沿。
2
3
4
5
6
7
8
row(默认值):主轴为水平方向,起点在左端
<template>
<mc-flex class="row" :direction="'row'">
<mc-item class="cont" :grow="1">1</mc-item>
<mc-item class="cont" :grow="1">2</mc-item>
<mc-item class="cont" :grow="1">3</mc-item>
</mc-flex>
</template>
2
3
4
5
6
7
row-reverse:主轴为水平方向,起点在右端
<template>
<mc-flex class="row" :direction="'row-reverse'">
<mc-item class="cont" :grow="1">1</mc-item>
<mc-item class="cont" :grow="1">2</mc-item>
<mc-item class="cont" :grow="1">3</mc-item>
</mc-flex>
</template>
2
3
4
5
6
7
column:主轴为垂直方向,起点在上沿
<template>
<mc-flex class="row" :direction="'column'">
<mc-item class="cont cont-column">1</mc-item>
<mc-item class="cont cont-column">2</mc-item>
<mc-item class="cont cont-column">3</mc-item>
</mc-flex>
</template>
2
3
4
5
6
7
column-reverse:主轴为垂直方向,起点在下沿
<template>
<mc-flex class="row" :direction="'column-reverse'">
<mc-item class="cont cont-column">1</mc-item>
<mc-item class="cont cont-column">2</mc-item>
<mc-item class="cont cont-column">3</mc-item>
</mc-flex>
</template>
2
3
4
5
6
7
# 2、wrap 换行
默认情况下,项目都排在一条线(又称"轴线")上。flex-wrap 属性定义,如果一条轴线排不下,如何换行。
wrap可以取三个值。
(1)nowrap(默认):不换行。
(2)wrap:换行,第一行在上方。
(3)wrap-reverse:换行,第一行在下方。
2
3
4
nowrap(默认):不换行
<template>
<mc-flex class="row" :wrap="'nowrap'">
<mc-item class="cont" :basis="'400px'">1</mc-item>
<mc-item class="cont" :basis="'400px'">2</mc-item>
<mc-item class="cont" :basis="'400px'">3</mc-item>
<mc-item class="cont" :basis="'400px'">4</mc-item>
<mc-item class="cont" :basis="'400px'">5</mc-item>
</mc-flex>
</template>
2
3
4
5
6
7
8
9
wrap:换行
<template>
<mc-flex class="row" :wrap="'wrap'">
<mc-item class="cont" :basis="'400px'">1</mc-item>
<mc-item class="cont" :basis="'400px'">2</mc-item>
<mc-item class="cont" :basis="'400px'">3</mc-item>
<mc-item class="cont" :basis="'400px'">4</mc-item>
<mc-item class="cont" :basis="'400px'">5</mc-item>
</mc-flex>
</template>
2
3
4
5
6
7
8
9
wrap-reverse:换行,第一行在下方
<template>
<mc-flex class="row" :wrap="'wrap-reverse'">
<mc-item class="cont" :basis="'400px'">1</mc-item>
<mc-item class="cont" :basis="'400px'">2</mc-item>
<mc-item class="cont" :basis="'400px'">3</mc-item>
<mc-item class="cont" :basis="'400px'">4</mc-item>
<mc-item class="cont" :basis="'400px'">5</mc-item>
</mc-flex>
</template>
2
3
4
5
6
7
8
9
# 3、对齐方式
justify 属性定义了项目在主轴上的对齐方式
它可能取 5 个值,具体对齐方式与轴的方向有关。下面假设主轴为从左到右。
flex-start(默认值):左对齐
flex-end:右对齐
center: 居中
space-between:两端对齐,项目之间的间隔都相等。
space-around:每个项目两侧的间隔相等。所以,项目之间的间隔比项目与边框的间隔大一倍。
2
3
4
5
flex-start(默认值):左对齐
<template>
<mc-flex class="row" :justify="'flex-start'">
<mc-item class="cont" :basis="'100px'">1</mc-item>
<mc-item class="cont" :basis="'100px'">2</mc-item>
<mc-item class="cont" :basis="'100px'">3</mc-item>
<mc-item class="cont" :basis="'100px'">4</mc-item>
<mc-item class="cont" :basis="'100px'">5</mc-item>
</mc-flex>
</template>
2
3
4
5
6
7
8
9
flex-end:右对齐
<template>
<mc-flex class="row" :justify="'flex-end'">
<mc-item class="cont" :basis="'100px'">1</mc-item>
<mc-item class="cont" :basis="'100px'">2</mc-item>
<mc-item class="cont" :basis="'100px'">3</mc-item>
<mc-item class="cont" :basis="'100px'">4</mc-item>
<mc-item class="cont" :basis="'100px'">5</mc-item>
</mc-flex>
</template>
2
3
4
5
6
7
8
9
center: 居中
<template>
<mc-flex class="row" :justify="'center'">
<mc-item class="cont" :basis="'100px'">1</mc-item>
<mc-item class="cont" :basis="'100px'">2</mc-item>
<mc-item class="cont" :basis="'100px'">3</mc-item>
<mc-item class="cont" :basis="'100px'">4</mc-item>
<mc-item class="cont" :basis="'100px'">5</mc-item>
</mc-flex>
</template>
2
3
4
5
6
7
8
9
space-between:两端对齐,项目之间的间隔都相等。
<template>
<mc-flex class="row" :justify="'space-between'">
<mc-item class="cont" :basis="'100px'">1</mc-item>
<mc-item class="cont" :basis="'100px'">2</mc-item>
<mc-item class="cont" :basis="'100px'">3</mc-item>
<mc-item class="cont" :basis="'100px'">4</mc-item>
<mc-item class="cont" :basis="'100px'">5</mc-item>
</mc-flex>
</template>
2
3
4
5
6
7
8
9
space-around:每个项目两侧的间隔相等。所以,项目之间的间隔比项目与边框的间隔大一倍。
<template>
<mc-flex class="row" :justify="'space-around'">
<mc-item class="cont" :basis="'100px'">1</mc-item>
<mc-item class="cont" :basis="'100px'">2</mc-item>
<mc-item class="cont" :basis="'100px'">3</mc-item>
<mc-item class="cont" :basis="'100px'">4</mc-item>
<mc-item class="cont" :basis="'100px'">5</mc-item>
</mc-flex>
</template>
2
3
4
5
6
7
8
9
# 4、alignItems
alignItems 属性定义项目在交叉轴上如何对齐
它可能取5个值。具体的对齐方式与交叉轴的方向有关,下面假设交叉轴从上到下。
flex-start:交叉轴的起点对齐。
flex-end:交叉轴的终点对齐。
center:交叉轴的中点对齐。
baseline: 项目的第一行文字的基线对齐。
stretch(默认值):如果项目未设置高度或设为auto,将占满整个容器的高度。
2
3
4
5
6
7
flex-start:交叉轴的起点对齐。
<template>
<mc-flex class="row row-align" :alignItems="'flex-start'">
<mc-item class="cont" :basis="'400px'">1</mc-item>
<mc-item class="cont" :basis="'400px'">2</mc-item>
<mc-item class="cont" :basis="'400px'">3</mc-item>
<mc-item class="cont" :basis="'400px'">4</mc-item>
<mc-item class="cont" :basis="'400px'">5</mc-item>
</mc-flex>
</template>
2
3
4
5
6
7
8
9
flex-end:交叉轴的终点对齐。
<template>
<mc-flex class="row row-align" :alignItems="'flex-end'">
<mc-item class="cont" :basis="'400px'">1</mc-item>
<mc-item class="cont" :basis="'400px'">2</mc-item>
<mc-item class="cont" :basis="'400px'">3</mc-item>
<mc-item class="cont" :basis="'400px'">4</mc-item>
<mc-item class="cont" :basis="'400px'">5</mc-item>
</mc-flex>
</template>
2
3
4
5
6
7
8
9
center:交叉轴的中点对齐。
<template>
<mc-flex class="row row-align" :alignItems="'center'">
<mc-item class="cont" :basis="'400px'">1</mc-item>
<mc-item class="cont" :basis="'400px'">2</mc-item>
<mc-item class="cont" :basis="'400px'">3</mc-item>
<mc-item class="cont" :basis="'400px'">4</mc-item>
<mc-item class="cont" :basis="'400px'">5</mc-item>
</mc-flex>
</template>
2
3
4
5
6
7
8
9
baseline: 项目的第一行文字的基线对齐。
<template>
<mc-flex class="row row-align" :alignItems="'baseline'">
<mc-item class="cont" :basis="'400px'">1</mc-item>
<mc-item class="cont" :basis="'400px'">2</mc-item>
<mc-item class="cont" :basis="'400px'">3</mc-item>
<mc-item class="cont" :basis="'400px'">4</mc-item>
<mc-item class="cont" :basis="'400px'">5</mc-item>
</mc-flex>
</template>
2
3
4
5
6
7
8
9
stretch(默认值):如果项目未设置高度或设为 auto,将占满整个容器的高度。
<template>
<mc-flex class="row row-align" :alignItems="'stretch'">
<mc-item class="cont" :basis="'400px'">1</mc-item>
<mc-item class="cont" :basis="'400px'">2</mc-item>
<mc-item class="cont" :basis="'400px'">3</mc-item>
<mc-item class="cont" :basis="'400px'">4</mc-item>
<mc-item class="cont" :basis="'400px'">5</mc-item>
</mc-flex>
</template>
2
3
4
5
6
7
8
9
# 5、alignContent
alignContent 属性定义了多根轴线的对齐方式。如果项目只有一根轴线,该属性不起作用
该属性可能取6个值。
flex-start:与交叉轴的起点对齐。
flex-end:与交叉轴的终点对齐。
center:与交叉轴的中点对齐。
space-between:与交叉轴两端对齐,轴线之间的间隔平均分布。
space-around:每根轴线两侧的间隔都相等。所以,轴线之间的间隔比轴线与边框的间隔大一倍。
stretch(默认值):轴线占满整个交叉轴。
2
3
4
5
6
7
8
flex-start:与交叉轴的起点对齐。
<template>
<mc-flex class="row row-align" :alignContent="'flex-start'" :wrap="'wrap'">
<mc-item class="cont" :basis="'400px'">1</mc-item>
<mc-item class="cont" :basis="'400px'">2</mc-item>
<mc-item class="cont" :basis="'400px'">3</mc-item>
<mc-item class="cont" :basis="'400px'">4</mc-item>
<mc-item class="cont" :basis="'400px'">5</mc-item>
</mc-flex>
</template>
2
3
4
5
6
7
8
9
flex-end:与交叉轴的终点对齐。
<template>
<mc-flex class="row row-align" :alignContent="'flex-end'" :wrap="'wrap'">
<mc-item class="cont" :basis="'400px'">1</mc-item>
<mc-item class="cont" :basis="'400px'">2</mc-item>
<mc-item class="cont" :basis="'400px'">3</mc-item>
<mc-item class="cont" :basis="'400px'">4</mc-item>
<mc-item class="cont" :basis="'400px'">5</mc-item>
</mc-flex>
</template>
2
3
4
5
6
7
8
9
center:与交叉轴的中点对齐。
<template>
<mc-flex class="row row-align" :alignContent="'space-between'" :wrap="'wrap'">
<mc-item class="cont" :basis="'400px'">1</mc-item>
<mc-item class="cont" :basis="'400px'">2</mc-item>
<mc-item class="cont" :basis="'400px'">3</mc-item>
<mc-item class="cont" :basis="'400px'">4</mc-item>
<mc-item class="cont" :basis="'400px'">5</mc-item>
</mc-flex>
</template>
2
3
4
5
6
7
8
9
space-between:与交叉轴两端对齐,轴线之间的间隔平均分布。
<template>
<mc-flex class="row row-align" :alignContent="'space-around'" :wrap="'wrap'">
<mc-item class="cont" :basis="'400px'">1</mc-item>
<mc-item class="cont" :basis="'400px'">2</mc-item>
<mc-item class="cont" :basis="'400px'">3</mc-item>
<mc-item class="cont" :basis="'400px'">4</mc-item>
<mc-item class="cont" :basis="'400px'">5</mc-item>
</mc-flex>
</template>
2
3
4
5
6
7
8
9
space-around:每根轴线两侧的间隔都相等。所以,轴线之间的间隔比轴线与边框的间隔大一倍。
<template>
<mc-flex class="row row-align" :alignContent="'space-around'" :wrap="'wrap'">
<mc-item class="cont" :basis="'400px'">1</mc-item>
<mc-item class="cont" :basis="'400px'">2</mc-item>
<mc-item class="cont" :basis="'400px'">3</mc-item>
<mc-item class="cont" :basis="'400px'">4</mc-item>
<mc-item class="cont" :basis="'400px'">5</mc-item>
</mc-flex>
</template>
2
3
4
5
6
7
8
9
stretch(默认值):轴线占满整个交叉轴。
<template>
<mc-flex class="row row-align" :alignContent="'stretch'" :wrap="'wrap'">
<mc-item class="cont" :basis="'400px'">1</mc-item>
<mc-item class="cont" :basis="'400px'">2</mc-item>
<mc-item class="cont" :basis="'400px'">3</mc-item>
<mc-item class="cont" :basis="'400px'">4</mc-item>
<mc-item class="cont" :basis="'400px'">5</mc-item>
</mc-flex>
</template>
2
3
4
5
6
7
8
9
# mc-item 的项目属性
# 1、order
order 属性定义项目的排列顺序。数值越小,排列越靠前,默认为 0。
数值越小,排列越靠前,默认为 0
<template>
<mc-flex class="row">
<mc-item class="cont" :order="3" :grow="1">1</mc-item>
<mc-item class="cont" :order="2" :grow="1">2</mc-item>
<mc-item class="cont" :order="1" :grow="1">3</mc-item>
</mc-flex>
</template>
2
3
4
5
6
7
# 2、grow
grow 属性定义项目的放大比例,默认为 0,即如果存在剩余空间,也不放大。
默认为 0,即如果存在剩余空间,也不放大
<template>
<mc-flex class="row">
<mc-item class="cont" :grow="1">1</mc-item>
<mc-item class="cont" :grow="2">2</mc-item>
<mc-item class="cont" :grow="3">3</mc-item>
</mc-flex>
</template>
2
3
4
5
6
7
# 3、shrink
shrink 属性定义了项目的缩小比例,默认为 1,即如果空间不足,该项目将缩小
默认为 1,即如果空间不足,该项目将缩小
<template>
<mc-flex class="row">
<mc-item class="cont" :shrink="1" :basis="'400px'">1</mc-item>
<mc-item class="cont" :shrink="2" :basis="'400px'">2</mc-item>
<mc-item class="cont" :shrink="3" :basis="'400px'">3</mc-item>
</mc-flex>
</template>
2
3
4
5
6
7
# 4、basis
basis 属性定义了在分配多余空间之前,项目占据的主轴空间(main size)。浏览器根据这个属性,计算主轴是否有多余空间。它的默认值为 auto,即项目的本来大小。
默认值为 auto,即项目的本来大小
<template>
<mc-flex class="row">
<mc-item class="cont" :basis="'100px'">1</mc-item>
<mc-item class="cont" :basis="'200px'">2</mc-item>
<mc-item class="cont" :basis="'300px'">3</mc-item>
</mc-flex>
</template>
2
3
4
5
6
7
# 5、alignSelf
alignSelf 属性允许单个项目有与其他项目不一样的对齐方式,可覆盖 alignItems 属性。 默认值为 auto,表示继承父元素的 alignItems 属性,如果没有父元素,则等同于 stretch。
auto | flex-start | flex-end | center | baseline | stretch
<template>
<mc-flex class="row" :alignItems="'flex-start'">
<mc-item class="cont" :alignSelf="'flex-end'" :basis="'100px'">1</mc-item>
<mc-item class="cont" :alignSelf="'center'" :basis="'200px'">2</mc-item>
<mc-item class="cont" :basis="'300px'">3</mc-item>
</mc-flex>
</template>
2
3
4
5
6
7
# 栅格布局属性(col Attributes)
参数 | 说明 | 类型 | 默认值 | 可选值 |
---|---|---|---|---|
span | 栅格占据的列数 | Number | 24 | - |
offset | 栅格左侧的间隔格数 | Number | 0 | - |
# flex 布局容器属性(Flex Attributes)
参数 | 说明 | 类型 | 默认值 | 可选值 |
---|---|---|---|---|
direction | 主轴的方向 | String | row | row、row-reverse、column、column-reverse |
wrap | 如何换行 | String | nowrap | nowrap、wrap、wrap-reverse |
justify | 主轴对齐方式 | String | flex-start | flex-start、flex-end、center、space-between、space-around |
alignItems | 交叉轴上如何对齐 | String | stretch | flex-start、flex-end、center、baseline、stretch |
alignContent | 多根轴线交叉轴上如何对齐 | String | stretch | flex-start、flex-end、center、space-between、space-around、stretch |
# flex 布局项目属性(Item Attributes)
参数 | 说明 | 类型 | 默认值 | 可选值 |
---|---|---|---|---|
order | 项目的排列顺序 | Number | 0 | - (数值越小,排列越靠前) |
grow | 项目的放大比例 | Number | 0 | -(默认为 0,即如果存在剩余空间,也不放大) |
shrink | 项目的缩小比例 | Number | 1 | - (默认为 1,即如果空间不足,该项目将缩小) |
basis | 项目在主轴上的大小 | String | auto | - (默认是auto,即项目的本来大小) |
alignSelf | 单个项目有与其他项目不一样的对齐方式 | String | auto | auto、flex-start、flex-end、center、baseline、stretch |