首页顶部一般会有多个”信息的块”, 针对不同分辨率, 会有不同的布局, 这种布局方式我们叫他:”响应式布局”.
image.png

image.png

栅格组件(Row/Col)

在”ant ui”中提供了响应布局的组件, ant中管这类组件叫”栅格“组件, 他用行/列划分界面. 所以栅格组件实际是2个组件, 一个”行组件“(Row), 一个”列组件“(Col).

默认1行(row)可以容纳24列(col), 所以大家可以通过设置每个Col组件的列数,实现元素在1行中的宽度占比, 可以通过使用”span“属性设置”占比”数字.
image.png

  1. <template>
  2. <a-row>
  3. <a-col :span="12">col-12</a-col>
  4. <a-col :span="12">col-12</a-col>
  5. </a-row>
  6. <a-row>
  7. <a-col :span="8">col-8</a-col>
  8. <a-col :span="8">col-8</a-col>
  9. <a-col :span="8">col-8</a-col>
  10. </a-row>
  11. <a-row>
  12. <a-col :span="6">col-6</a-col>
  13. <a-col :span="6">col-6</a-col>
  14. <a-col :span="6">col-6</a-col>
  15. <a-col :span="6">col-6</a-col>
  16. </a-row>
  17. </template>

响应式(xs / sm / md / lg / xl / xxl / xxxl)

因为我们的需求是浏览器不同分辨率下对每个元素设置不同的宽度, 那么显然”span“是不够用的, 还好ant提供了类似”Bootstrap”的响应式布局. 我们可以通过”xs / sm / md / lg / xl / xxl / xxxl“等属性定义不同分辨率下span(每个列的宽度占比).

  1. <template>
  2. <a-row>
  3. <a-col :xs="2" :sm="4" :md="6" :lg="8" :xl="10">Col</a-col>
  4. <a-col :xs="20" :sm="16" :md="12" :lg="8" :xl="4">Col</a-col>
  5. <a-col :xs="2" :sm="4" :md="6" :lg="8" :xl="10">Col</a-col>
  6. </a-row>
  7. </template>

image.png

间隙(gutter)

通过Row上的”gutter”属性, 可以设置列之间的间距.

  1. <template>
  2. <div class="gutter-example">
  3. <a-row :gutter="16">
  4. <a-col class="gutter-row" :span="6">
  5. <div class="gutter-box">col-6</div>
  6. </a-col>
  7. <a-col class="gutter-row" :span="6">
  8. <div class="gutter-box">col-6</div>
  9. </a-col>
  10. <a-col class="gutter-row" :span="6">
  11. <div class="gutter-box">col-6</div>
  12. </a-col>
  13. <a-col class="gutter-row" :span="6">
  14. <div class="gutter-box">col-6</div>
  15. </a-col>
  16. </a-row>
  17. </div>
  18. </template>

image.png

对齐方式

Row组件可以通过”justify/align”属性设置Col组件的水平/垂直对齐方式, 实际Row组件内部就是使用了flex布局, 通过”align-items“和”justify-content“实现对齐. 所以取值就是对应的css属性值.

  1. <a-row type="flex" justify="center" align="top">
  2. <a-col :span="4">
  3. <p class="height-100">col-4</p>
  4. </a-col>
  5. <a-col :span="4">
  6. <p class="height-50">col-4</p>
  7. </a-col>
  8. <a-col :span="4">
  9. <p class="height-120">col-4</p>
  10. </a-col>
  11. <a-col :span="4">
  12. <p class="height-80">col-4</p>
  13. </a-col>
  14. </a-row>

这里不展开讲解参数, 开发时去查文档即可, 这里只需要知道有”对齐方式”功能即可. 官方文档

image.png

image.png