Layout 布局

介绍

Layout 提供了 van-rowvan-col 两个组件来进行行列布局。

引入

通过以下方式来全局注册组件,更多注册方式请参考组件注册

  1. import { createApp } from 'vue';
  2. import { Col, Row } from 'vant';
  3. const app = createApp();
  4. app.use(Col);
  5. app.use(Row);

代码演示

基础用法

Layout 组件提供了 24列栅格,通过在 Col 上添加 span 属性设置列所占的宽度百分比。此外,添加 offset 属性可以设置列的偏移宽度,计算方式与 span 相同。

  1. <van-row>
  2. <van-col span="8">span: 8</van-col>
  3. <van-col span="8">span: 8</van-col>
  4. <van-col span="8">span: 8</van-col>
  5. </van-row>
  6. <van-row>
  7. <van-col span="4">span: 4</van-col>
  8. <van-col span="10" offset="4">offset: 4, span: 10</van-col>
  9. </van-row>
  10. <van-row>
  11. <van-col offset="12" span="12">offset: 12, span: 12</van-col>
  12. </van-row>

设置列元素间距

通过 gutter 属性可以设置列元素之间的间距,默认间距为 0。

  1. <van-row gutter="20">
  2. <van-col span="8">span: 8</van-col>
  3. <van-col span="8">span: 8</van-col>
  4. <van-col span="8">span: 8</van-col>
  5. </van-row>

对齐方式

通过 justify 属性可以设置主轴上内容的对齐方式,等价于 flex 布局中的 justify-content 属性。

  1. <!-- 居中 -->
  2. <van-row justify="center">
  3. <van-col span="6">span: 6</van-col>
  4. <van-col span="6">span: 6</van-col>
  5. <van-col span="6">span: 6</van-col>
  6. </van-row>
  7. <!-- 右对齐 -->
  8. <van-row justify="end">
  9. <van-col span="6">span: 6</van-col>
  10. <van-col span="6">span: 6</van-col>
  11. <van-col span="6">span: 6</van-col>
  12. </van-row>
  13. <!-- 两端对齐 -->
  14. <van-row justify="space-between">
  15. <van-col span="6">span: 6</van-col>
  16. <van-col span="6">span: 6</van-col>
  17. <van-col span="6">span: 6</van-col>
  18. </van-row>
  19. <!-- 每个元素的两侧间隔相等 -->
  20. <van-row justify="space-around">
  21. <van-col span="6">span: 6</van-col>
  22. <van-col span="6">span: 6</van-col>
  23. <van-col span="6">span: 6</van-col>
  24. </van-row>

API

Row Props

参数说明类型默认值
gutter列元素之间的间距(单位为 px)number | string-
tag自定义元素标签stringdiv
justify主轴对齐方式,可选值为 end center
space-around space-between
stringstart
align交叉轴对齐方式,可选值为 center bottomstringtop
wrap v3.0.11是否自动换行booleantrue

Col Props

参数说明类型默认值
span列元素宽度number | string-
offset列元素偏移距离number | string-
tag自定义元素标签stringdiv

Row Events

事件名说明回调参数
click点击时触发event: MouseEvent

Col Events

事件名说明回调参数
click点击时触发event: MouseEvent

类型定义

组件导出以下类型定义:

  1. import type { RowAlign, RowJustify } from 'vant';

Layout 布局 - 图1