Layout 布局

介绍

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

引入

  1. import Vue from 'vue';
  2. import { Col, Row } from 'vant';
  3. Vue.use(Col);
  4. Vue.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>

Flex 布局

type 属性设置为 flex 可以启用 flex 布局,便于进行灵活的对齐

  1. <!-- 左对齐 -->
  2. <van-row type="flex">
  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 type="flex" justify="center">
  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 type="flex" justify="end">
  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 type="flex" justify="space-between">
  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>
  25. <!-- 每个元素的两侧间隔相等 -->
  26. <van-row type="flex" justify="space-around">
  27. <van-col span="6">span: 6</van-col>
  28. <van-col span="6">span: 6</van-col>
  29. <van-col span="6">span: 6</van-col>
  30. </van-row>

API

Row Props

参数说明类型默认值
type布局方式,可选值为flexstring-
gutter列元素之间的间距(单位为 px)number | string-
tag自定义元素标签stringdiv
justifyFlex 主轴对齐方式,可选值为 end center
space-around space-between
stringstart
alignFlex 交叉轴对齐方式,可选值为 center bottomstringtop

Col Props

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

Row Events

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

Col Events

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

Layout 布局 - 图1