Grid 栅格
把一行分成 24 列,用 HuiRow 包一行、HuiCol 占其中几列,快速搭出按比例分栏、随屏幕宽度变化的布局。
引入
import { HuiRow, HuiCol } from '@nscode/handy-ui'
基础用法
HuiCol 的 span 是这一列占的列数,一行一共 24 列。一行里各列的 span 加起来超过 24 时,多出来的列换到下一行。
<script setup lang="ts">
import { HuiRow, HuiCol, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiRow>
<HuiCol :span="24"><div class="cell">24</div></HuiCol>
</HuiRow>
<HuiRow>
<HuiCol :span="12"><div class="cell">12</div></HuiCol>
<HuiCol :span="12"><div class="cell cell--light">12</div></HuiCol>
</HuiRow>
<HuiRow>
<HuiCol :span="8"><div class="cell">8</div></HuiCol>
<HuiCol :span="8"><div class="cell cell--light">8</div></HuiCol>
<HuiCol :span="8"><div class="cell">8</div></HuiCol>
</HuiRow>
</HuiSpace>
</template>
<style scoped>
.cell {
height: 36px;
line-height: 36px;
text-align: center;
background: var(--hui-fill-color);
}
.cell--light {
background: var(--hui-fill-color-light);
}
</style>
.cell 只是给格子加底色、方便看出每一列的位置,和栅格本身无关,后面每个示例都带着同样的样式。
span 可以是 0~24 之间的任意整数,同一行里的列可以宽窄不一:
<template>
<HuiSpace direction="vertical" fill>
<HuiRow>
<HuiCol v-for="i in 4" :key="i" :span="6">
<div :class="['cell', { 'cell--light': i % 2 === 0 }]">6</div>
</HuiCol>
</HuiRow>
<HuiRow>
<HuiCol v-for="i in 6" :key="i" :span="4">
<div :class="['cell', { 'cell--light': i % 2 === 0 }]">4</div>
</HuiCol>
</HuiRow>
<HuiRow>
<HuiCol v-for="i in 8" :key="i" :span="3">
<div :class="['cell', { 'cell--light': i % 2 === 0 }]">3</div>
</HuiCol>
</HuiRow>
<HuiRow>
<HuiCol :span="16"><div class="cell">16</div></HuiCol>
<HuiCol :span="8"><div class="cell cell--light">8</div></HuiCol>
</HuiRow>
<HuiRow>
<HuiCol :span="4"><div class="cell">4</div></HuiCol>
<HuiCol :span="16"><div class="cell cell--light">16</div></HuiCol>
<HuiCol :span="4"><div class="cell">4</div></HuiCol>
</HuiRow>
<HuiRow>
<HuiCol :span="16"><div class="cell">16</div></HuiCol>
<HuiCol :span="16"
><div class="cell cell--light">16(加起来超过 24,换到下一行)</div></HuiCol
>
</HuiRow>
</HuiSpace>
</template>
<style scoped>
.cell {
height: 36px;
line-height: 36px;
text-align: center;
background: var(--hui-fill-color);
}
.cell--light {
background: var(--hui-fill-color-light);
}
</style>
列间距
HuiRow 的 gutter 设置列与列之间的间距,单位 px。传数组 [水平, 垂直] 时还会加上行与行之间的间距,适合一行放不下、换行显示的场景。
<template>
<HuiSpace direction="vertical" fill>
<HuiRow :gutter="16">
<HuiCol :span="6"><div class="cell">6</div></HuiCol>
<HuiCol :span="6"><div class="cell">6</div></HuiCol>
<HuiCol :span="6"><div class="cell">6</div></HuiCol>
<HuiCol :span="6"><div class="cell">6</div></HuiCol>
</HuiRow>
<HuiRow :gutter="[16, 16]">
<HuiCol v-for="i in 8" :key="i" :span="6"
><div class="cell">{{ i }}</div></HuiCol
>
</HuiRow>
</HuiSpace>
</template>
<style scoped>
.cell {
height: 36px;
line-height: 36px;
text-align: center;
background: var(--hui-fill-color);
}
</style>
间距是这样实现的:每个 HuiCol 左右各加 gutter / 2 的内边距,HuiRow 左右各用 -gutter / 2 的外边距抵消掉,所以第一列和最后一列的内容仍然和行的边缘对齐。列的背景要画在 HuiCol 里面的元素上,画在 HuiCol 上会把间距也涂上颜色。
不同的间距放在一起对比:
<template>
<HuiSpace direction="vertical" fill>
<div v-for="gutter in [0, 8, 16, 32]" :key="gutter">
<div class="label">gutter {{ gutter }}px</div>
<HuiRow :gutter="gutter">
<HuiCol v-for="i in 4" :key="i" :span="6"><div class="cell">6</div></HuiCol>
</HuiRow>
</div>
</HuiSpace>
</template>
<style scoped>
.label {
margin-bottom: 4px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
.cell {
height: 36px;
line-height: 36px;
text-align: center;
background: var(--hui-fill-color);
}
</style>
运行中修改 gutter,各列的间距跟着变。拖动滑块分别调整水平和垂直间距:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCol, HuiRow, HuiSlider, HuiSpace } from '@nscode/handy-ui'
const horizontal = ref(16)
const vertical = ref(16)
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace wrap size="large">
<span>水平 {{ horizontal }}px</span>
<HuiSlider
v-model="horizontal"
:min="0"
:max="48"
:step="8"
show-stops
style="width: 200px"
/>
<span>垂直 {{ vertical }}px</span>
<HuiSlider v-model="vertical" :min="0" :max="48" :step="8" show-stops style="width: 200px" />
</HuiSpace>
<HuiRow :gutter="[horizontal, vertical]">
<HuiCol v-for="i in 8" :key="i" :span="6">
<div class="cell">{{ i }}</div>
</HuiCol>
</HuiRow>
</HuiSpace>
</template>
<style scoped>
.cell {
height: 36px;
line-height: 36px;
text-align: center;
background: var(--hui-fill-color);
}
</style>
列偏移
offset 让列向右空出指定的列数,偏移的列数和 span 一样是 0~24。
<template>
<HuiSpace direction="vertical" fill>
<HuiRow>
<HuiCol :span="6"><div class="cell">6</div></HuiCol>
<HuiCol :span="6" :offset="6"><div class="cell">6,偏移 6</div></HuiCol>
</HuiRow>
<HuiRow>
<HuiCol :span="12" :offset="6"><div class="cell">12,偏移 6(居中)</div></HuiCol>
</HuiRow>
<HuiRow v-for="offset in [0, 4, 8, 12, 16, 20]" :key="offset">
<HuiCol :span="4" :offset="offset">
<div class="cell cell--light">偏移 {{ offset }}</div>
</HuiCol>
</HuiRow>
<HuiRow>
<HuiCol :span="6" :offset="2"><div class="cell">6,偏移 2</div></HuiCol>
<HuiCol :span="6" :offset="2"><div class="cell">6,偏移 2</div></HuiCol>
<HuiCol :span="6" :offset="2"><div class="cell">6,偏移 2</div></HuiCol>
</HuiRow>
</HuiSpace>
</template>
<style scoped>
.cell {
height: 36px;
line-height: 36px;
text-align: center;
background: var(--hui-fill-color);
}
.cell--light {
background: var(--hui-fill-color-light);
}
</style>
调换顺序
push 把列向右推、pull 把列向左拉,单位是列数。只移动显示位置,不影响其他列的位置,常用来在不改模板顺序的前提下调换两列。
<template>
<HuiRow>
<HuiCol :span="18" :push="6"><div class="cell">在模板里排第一,显示在右边</div></HuiCol>
<HuiCol :span="6" :pull="18"
><div class="cell cell--light">在模板里排第二,显示在左边</div></HuiCol
>
</HuiRow>
</template>
<style scoped>
.cell {
height: 36px;
line-height: 36px;
text-align: center;
background: var(--hui-fill-color);
}
.cell--light {
background: var(--hui-fill-color-light);
}
</style>
水平排列
HuiRow 的 justify 控制一行里各列的水平分布,可选 start(默认)、end、center、space-around、space-between、space-evenly。只有各列加起来不满 24 列、一行还有空余时才看得出区别。
<script setup lang="ts">
import { HuiCol, HuiRow, HuiSpace } from '@nscode/handy-ui'
import type { RowJustify } from '@nscode/handy-ui'
const justifies: RowJustify[] = [
'start',
'end',
'center',
'space-around',
'space-between',
'space-evenly'
]
</script>
<template>
<HuiSpace direction="vertical" fill>
<div v-for="item in justifies" :key="item">
<div class="label">{{ item }}</div>
<HuiRow :justify="item" class="row">
<HuiCol :span="4"><div class="cell">4</div></HuiCol>
<HuiCol :span="4"><div class="cell cell--light">4</div></HuiCol>
<HuiCol :span="4"><div class="cell">4</div></HuiCol>
</HuiRow>
</div>
</HuiSpace>
</template>
<style scoped>
.label {
margin-bottom: 4px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
.row {
outline: 1px dashed var(--hui-border-color);
}
.cell {
height: 36px;
line-height: 36px;
text-align: center;
background: var(--hui-fill-color);
}
.cell--light {
background: var(--hui-fill-color-dark);
}
</style>
垂直对齐
HuiRow 的 align 控制各列在垂直方向上的对齐,可选 top(默认)、middle、bottom、stretch。stretch 让每一列都拉到这一行最高的那一列的高度。
<template>
<HuiRow align="middle" :gutter="16">
<HuiCol :span="8"><div class="cell" style="height: 80px">高 80px</div></HuiCol>
<HuiCol :span="8"><div class="cell">垂直居中</div></HuiCol>
<HuiCol :span="8"><div class="cell" style="height: 50px">高 50px</div></HuiCol>
</HuiRow>
</template>
<style scoped>
.cell {
height: 36px;
line-height: 36px;
text-align: center;
background: var(--hui-fill-color);
}
</style>
四个值放在一起对比。每行左边一列高 80px,中间一列没设高度(拉伸时高度跟着列变),右边一列高 50px:
<script setup lang="ts">
import { HuiCol, HuiRow, HuiSpace } from '@nscode/handy-ui'
import type { RowAlign } from '@nscode/handy-ui'
const aligns: RowAlign[] = ['top', 'middle', 'bottom', 'stretch']
</script>
<template>
<HuiSpace direction="vertical" fill>
<div v-for="item in aligns" :key="item">
<div class="label">{{ item }}</div>
<HuiRow :align="item" :gutter="16">
<HuiCol :span="8"><div class="cell" style="height: 80px">高 80px</div></HuiCol>
<HuiCol :span="8"><div class="cell cell--fit">不设高度</div></HuiCol>
<HuiCol :span="8"><div class="cell" style="height: 50px">高 50px</div></HuiCol>
</HuiRow>
</div>
</HuiSpace>
</template>
<style scoped>
.label {
margin-bottom: 4px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
.cell {
display: flex;
align-items: center;
justify-content: center;
min-height: 36px;
box-sizing: border-box;
background: var(--hui-fill-color);
}
/* 列被拉伸时,高度 100% 才能跟着列变高 */
.cell--fit {
height: 100%;
background: var(--hui-fill-color-dark);
}
</style>
不换行
HuiRow 默认允许换行。wrap 为 false 时所有列排在同一行,列不会被压窄,span 加起来超过 24 时会超出这一行。
<template>
<HuiRow :wrap="false">
<HuiCol :span="12"><div class="cell">12</div></HuiCol>
<HuiCol :span="12"><div class="cell cell--light">12</div></HuiCol>
<HuiCol :span="12"><div class="cell">12</div></HuiCol>
</HuiRow>
</template>
<style scoped>
.cell {
height: 36px;
line-height: 36px;
text-align: center;
background: var(--hui-fill-color);
}
.cell--light {
background: var(--hui-fill-color-light);
}
</style>
同样三列 12,默认换行和不换行对比(下面给行外面套了横向滚动的容器,看得到超出的部分):
<template>
<HuiSpace direction="vertical" fill>
<div>
<div class="label">wrap(默认):第三列换到下一行</div>
<HuiRow>
<HuiCol v-for="i in 3" :key="i" :span="12">
<div :class="['cell', { 'cell--light': i % 2 === 0 }]">12</div>
</HuiCol>
</HuiRow>
</div>
<div>
<div class="label">:wrap="false":第三列超出这一行</div>
<div style="overflow-x: auto">
<HuiRow :wrap="false">
<HuiCol v-for="i in 3" :key="i" :span="12">
<div :class="['cell', { 'cell--light': i % 2 === 0 }]">12</div>
</HuiCol>
</HuiRow>
</div>
</div>
</HuiSpace>
</template>
<style scoped>
.label {
margin-bottom: 4px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
.cell {
height: 36px;
line-height: 36px;
text-align: center;
background: var(--hui-fill-color);
}
.cell--light {
background: var(--hui-fill-color-light);
}
</style>
宽度随内容和隐藏列
span 不填时,列的宽度等于里面内容的宽度。span 为 0 时这一列不显示。
<template>
<HuiRow :gutter="8">
<HuiCol><div class="cell" style="padding: 0 12px">宽度随内容</div></HuiCol>
<HuiCol :span="0"><div class="cell">不显示</div></HuiCol>
<HuiCol :span="8"><div class="cell cell--light">8</div></HuiCol>
</HuiRow>
</template>
<style scoped>
.cell {
height: 36px;
line-height: 36px;
text-align: center;
background: var(--hui-fill-color);
}
.cell--light {
background: var(--hui-fill-color-light);
}
</style>
响应式布局
xs、sm、md、lg、xl 按屏幕宽度设置列宽,值可以是数字(等同于 span),也可以是对象 { span, offset, push, pull }。各断点的生效范围:
| 断点 | 生效范围 |
|---|---|
| xs | <640px,只在这个范围内 |
| sm | ≥640px 起生效,向上继承 |
| md | ≥768px 起生效,向上继承 |
| lg | ≥1024px 起生效,向上继承 |
| xl | ≥1280px 起生效,向上继承 |
“向上继承”的意思是:断点设置的值在比它更宽的屏幕上一直有效,直到被更大的断点覆盖。span、offset 等不带断点的属性在所有宽度下都有效,被断点的值覆盖。
下面这个例子:小于 768px 时每列占满一行;768px 起两列一行;1280px 起四列一行。
<template>
<HuiRow :gutter="[16, 16]">
<HuiCol v-for="i in 4" :key="i" :span="24" :md="12" :xl="6">
<div class="cell">{{ i }}</div>
</HuiCol>
</HuiRow>
</template>
<style scoped>
.cell {
height: 36px;
line-height: 36px;
text-align: center;
background: var(--hui-fill-color);
}
</style>
五个断点都写上时,每个宽度范围各有自己的列宽。下面显示当前窗口宽度和生效的断点,拖动浏览器窗口改变宽度看效果:
<script setup lang="ts">
import { computed, onBeforeUnmount, onMounted, ref } from 'vue'
import { HuiCol, HuiRow, HuiSpace } from '@nscode/handy-ui'
const width = ref(0)
const updateWidth = () => {
width.value = window.innerWidth
}
onMounted(() => {
updateWidth()
window.addEventListener('resize', updateWidth)
})
onBeforeUnmount(() => window.removeEventListener('resize', updateWidth))
const current = computed(() => {
if (width.value < 640) return 'xs(每列 24,一行 1 列)'
if (width.value < 768) return 'sm(每列 12,一行 2 列)'
if (width.value < 1024) return 'md(每列 8,一行 3 列)'
if (width.value < 1280) return 'lg(每列 6,一行 4 列)'
return 'xl(每列 4,一行 6 列)'
})
</script>
<template>
<HuiSpace direction="vertical" fill>
<span>窗口宽度 {{ width }}px,生效的断点是 {{ current }}</span>
<HuiRow :gutter="[16, 16]">
<HuiCol v-for="i in 6" :key="i" :xs="24" :sm="12" :md="8" :lg="6" :xl="4">
<div :class="['cell', { 'cell--light': i % 2 === 0 }]">{{ i }}</div>
</HuiCol>
</HuiRow>
</HuiSpace>
</template>
<style scoped>
.cell {
height: 36px;
line-height: 36px;
text-align: center;
background: var(--hui-fill-color);
}
.cell--light {
background: var(--hui-fill-color-light);
}
</style>
用对象形式时可以在某个断点同时改偏移。下面这一列在 1024px 以下占满一行,1024px 起占 12 列并居中:
<template>
<HuiRow>
<HuiCol :span="24" :lg="{ span: 12, offset: 6 }">
<div class="cell">响应式偏移</div>
</HuiCol>
</HuiRow>
</template>
<style scoped>
.cell {
height: 36px;
line-height: 36px;
text-align: center;
background: var(--hui-fill-color);
}
</style>
对象里的 push、pull 可以只在宽屏上调换顺序。下面在 1024px 以下主内容在上、侧栏在下;1024px 起侧栏挪到左边:
<template>
<HuiRow :gutter="[16, 16]">
<HuiCol :span="24" :lg="{ span: 16, push: 8 }">
<div class="cell">主内容(模板里排第一)</div>
</HuiCol>
<HuiCol :span="24" :lg="{ span: 8, pull: 16 }">
<div class="cell cell--light">侧栏(模板里排第二)</div>
</HuiCol>
</HuiRow>
</template>
<style scoped>
.cell {
height: 36px;
line-height: 36px;
text-align: center;
background: var(--hui-fill-color);
}
.cell--light {
background: var(--hui-fill-color-light);
}
</style>
某个断点设成 0 可以在这个宽度及以上隐藏这一列,更大的断点设成非零值又会显示出来:
<template>
<HuiRow :gutter="16">
<HuiCol :span="6" :md="0" :xl="6"><div class="cell">768~1279px 隐藏</div></HuiCol>
<HuiCol :span="18" :md="24" :xl="18"><div class="cell cell--light">主内容</div></HuiCol>
</HuiRow>
</template>
<style scoped>
.cell {
height: 36px;
line-height: 36px;
text-align: center;
background: var(--hui-fill-color);
}
.cell--light {
background: var(--hui-fill-color-light);
}
</style>
自定义标签
HuiRow、HuiCol 默认渲染成 div,tag 可以换成别的元素,例如列表结构用 ul 和 li。
<template>
<HuiRow tag="ul" :gutter="16" style="padding: 0; list-style: none">
<HuiCol v-for="i in 3" :key="i" tag="li" :span="8"
><div class="cell">{{ i }}</div></HuiCol
>
</HuiRow>
</template>
<style scoped>
.cell {
height: 36px;
line-height: 36px;
text-align: center;
background: var(--hui-fill-color);
}
</style>
HuiRow Props
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| gutter | number | [number, number] | 0 | 列间距,单位 px。数组是 [水平, 垂直],垂直间距是行与行之间的距离 |
| justify | 'start' | 'end' | 'center' | 'space-around' | 'space-between' | 'space-evenly' | 'start' | 各列的水平分布 |
| align | 'top' | 'middle' | 'bottom' | 'stretch' | 'top' | 各列的垂直对齐 |
| wrap | boolean | true | 放不下时是否换行 |
| tag | string | 'div' | 渲染成的元素 |
HuiCol Props
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| span | number | - | 占的列数,0~24。不填时宽度等于内容宽度;为 0 时隐藏 |
| offset | number | - | 左侧空出的列数 |
| push | number | - | 向右移动的列数,不影响其他列的位置 |
| pull | number | - | 向左移动的列数,不影响其他列的位置 |
| xs | number | ColResponsiveProps | - | 屏幕宽度 <640px 时的设置,只在这个范围内生效。数字等同于 span |
| sm | number | ColResponsiveProps | - | ≥640px 起生效,向上继承。数字等同于 span |
| md | number | ColResponsiveProps | - | ≥768px 起生效,向上继承。数字等同于 span |
| lg | number | ColResponsiveProps | - | ≥1024px 起生效,向上继承。数字等同于 span |
| xl | number | ColResponsiveProps | - | ≥1280px 起生效,向上继承。数字等同于 span |
| tag | string | 'div' | 渲染成的元素 |
HuiRow Slots
| 插槽 | 作用域参数 | 说明 |
|---|---|---|
| default | - | 放 HuiCol |
HuiCol Slots
| 插槽 | 作用域参数 | 说明 |
|---|---|---|
| default | - | 列的内容 |
类型
import type { RowProps, ColProps, ColResponsiveProps, RowJustify, RowAlign } from '@nscode/handy-ui'
type RowJustify = 'start' | 'end' | 'center' | 'space-around' | 'space-between' | 'space-evenly'
type RowAlign = 'top' | 'middle' | 'bottom' | 'stretch'
interface RowProps {
gutter?: number | [number, number]
justify?: RowJustify
align?: RowAlign
wrap?: boolean
tag?: string
}
/** 断点属性写成对象时的格式 */
interface ColResponsiveProps {
span?: number
offset?: number
push?: number
pull?: number
}
interface ColProps extends ColResponsiveProps {
xs?: number | ColResponsiveProps
sm?: number | ColResponsiveProps
md?: number | ColResponsiveProps
lg?: number | ColResponsiveProps
xl?: number | ColResponsiveProps
tag?: string
}
另外还导出了 RowContext 和注入用的 ROW_INJECTION_KEY,HuiCol 通过它拿到所在行的 gutter,一般用不到。