Waterfall 瀑布流
按预估高度把条目放入最短列;追加保留旧项位置,切换 listKey 重新排布。
引入
uni-app 配置 easycom 后使用 <hui-waterfall>。公开类型和函数从 @nscode/handy-uni 导入;普通 Vue 3 安装 @nscode/handy-uni/web 预览插件。
示例
示例可独立运行;交互结果直接显示在页面。
vue
<template>
<view>
<hui-space direction="vertical" fill>
<hui-waterfall
custom-class="hui-space__item"
:items="items"
item-key="id"
:list-key="version"
:estimate-height="estimateHeight"
@layout="(value) => (lastEvent = JSON.stringify(value))"
>
<template #item="{ index, column }"
><hui-card :title="items[index].title"
><text>{{ column }} / {{ items[index].height }}</text></hui-card
></template
>
</hui-waterfall>
<hui-button custom-class="hui-space__item" @click="append">追加 / Append</hui-button
><hui-button custom-class="hui-space__item" @click="reset"
>更换列表 / Replace list</hui-button
>
<text class="hui-space__item">{{ lastEvent }}</text>
</hui-space>
</view>
</template>
<script setup lang="ts">
import { ref } from 'vue'
const lastEvent = ref('尚无事件 / No event')
const version = ref(0)
const items = ref([
{ id: 1, title: '甲 / A', height: 140 },
{ id: 2, title: '乙 / B', height: 90 }
])
const estimateHeight = (item: unknown) => (item as { height: number }).height
const append = () => {
const id = items.value.length + 1
items.value.push({ id, title: '条目 / Item ' + id, height: 110 })
}
const reset = () => {
version.value++
items.value = [{ id: 1, title: '新列表 / New list', height: 180 }]
}
</script>
完整内容流
组合轮播预览、图片失败回退与重试、瀑布流追加、筛选换代、长文展开、文章正文、底部动作栏和分享。业务页可在触底回调中调用 append。
vue
<template>
<view>
<hui-space direction="vertical" fill>
<hui-page-host custom-class="hui-space__item" />
<hui-swiper
custom-class="hui-space__item"
:items="banners"
:height="160"
indicator-type="fraction"
preview
:autoplay="false"
/>
<hui-button custom-class="hui-space__item" @click="filter"
>切换筛选 / Change filter</hui-button
>
<hui-waterfall
custom-class="hui-space__item"
:items="cards"
item-key="id"
:list-key="listKey"
:estimate-height="estimateHeight"
>
<template #item="{ index }">
<hui-card :title="cards[index].title">
<hui-image
:src="cards[index].failed ? '/missing-cover.png' : cards[index].src"
:fallback-src="cards[index].src"
width="100%"
:ratio="cards[index].ratio"
>
<template #error="{ reload }"
><hui-button @click.stop="reload">重试 / Retry</hui-button></template
>
</hui-image>
<hui-show-more :text="cards[index].text" :rows="2" />
</hui-card>
</template>
</hui-waterfall>
<hui-button custom-class="hui-space__item" @click="append"
>触底后追加 / Append next page</hui-button
>
<hui-rich-text
custom-class="hui-space__item"
content="<p><strong>文章正文 / Article</strong></p><p>图片按宽高预估布局,追加不移动旧项。 / Estimate from image ratios; appending keeps existing cards in place.</p>"
/>
<hui-action-bar
custom-class="hui-space__item"
:fixed="false"
:icons="[{ icon: 'share', text: '分享 / Share' }]"
@icon-click="shareVisible = true"
/>
<hui-share-sheet
v-model:visible="shareVisible"
custom-class="hui-space__item"
:options="shareOptions"
:payload="payload"
/>
<text class="hui-space__item">{{ lastEvent }}</text>
</hui-space>
</view>
</template>
<script setup lang="ts">
import { ref } from 'vue'
const lastEvent = ref('尚无事件 / No event')
const banners = [
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAgCAIAAADbtmxLAAAB/0lEQVR4nM3QVUIUABAA0DmEgCDYgoCBqEgoiIiBSkp3d9d2dy/d3d3d3SWooBJ2YAL/XmPeER4ccyaoupKOu1M0PGma3oyTvqzT/pyzgbzzwQLtUNHFcIlepOxStOJKbJpBfIZhYtaN5Byj1DxjYoEpueg2tcScXnaXWXGPXXWfW/OAX/dI2GAjbnoqbbGVt9krOxzTu55n9rhk97nlDnjkD3kVjvgUj/mVTgSUTwVVzoRUz4XVLkTUL0U1rsQ0r8W1rie0vwQVF6KaG1ndg3rCi67lwzzlxz4TwD0XxL8QItQJE+tGSPWj5JdjlFfj0q8lZF5Pyr6ZknuLkG9CKjSjFN+hlVowyi1ZlVacamte7UNB/WNR4xNJ8zNZq52i3SGt0ymj2zmr1zWn3z1v0LNg2Lto1Ldk3L9sMrBiOrhqNrRmPrxuMbJhObppNbblRXzbRmLHK0DVk9T5GlD1JHdtAqqelO4tQNWT2vMGUPUQet8Cqh5i3ztA1UPq3wZUPeSBHUDVQxncBVQ91KE9QNVDG34PqHroIx8AVQ9j9COg6mGOfQJUPazxz4Cqhz3xBVD1cCa/Aqoe7tQ3QNXDm/4OqHr4Mz8AVY9gdh9Q9QjnfgKqHtH8L0DVI174Dah6JIt/AFWPdOkvoOqRLf8DVD3ylQNA1aNYPQRUPcq1o/83VquIElJ47QAAAABJRU5ErkJggg==',
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAgCAIAAADbtmxLAAAB30lEQVR4nM3QA2IcABAAwP1Qbdt2G+Ns27Zt5+5i27aTGj/pN3aeMFBzglB3itRwhtJ0jtZygUG4xCJd4VCu8Wg3BIxbItYdCeeejPdAIXikEj3RSJ7pZC8Milcm1RuL5p1N98Fh+OQyffFYan22+oCjMeRqjnhaYz5iIkBOhaiZCD0XYxYS7FKKW87wqzlhR0HcVZL2lOV9VeVAh3qoSzvSox/rM04MmKeGrDMj9jmoPUmsP01uPEttPk9vvcgkXmaTr3Kp1/n0m0LmbTH7rpR7X85/qBQ+VoufaqXP9fKXRuVrs/qtVfverv/oNH52m2u81jq/vSHobAq7W6JeQtxPSgYp6TAtG2Xk46xiktOW5lWygva8qLMo6W6T9VYU/e2qwU7NcLdutNcw3m+aHLRMD9tmRx3zgKpnzLkAqHrGXYuAqmfCvQSoeiY9y4CqZ8q7Aqh6pn2rgKpnxr8GqHpmA+uAqmcuuAGoeuZDm4CqZyG8Bah6FiPbgKpnKboDqHqWY7uAqmclvgeoelYT+4CqZy15AKh61lOHgKpnI30EqHo2M8eAqmcr+xVQ9WznvgGqnp38d0DVs1v4Aah69oo/AVXPfukXoOo5aPsNqHoOy38AVc9R5S+g6jmu/vsPM1TniEVFeWsAAAAASUVORK5CYII='
]
const listKey = ref(0)
const shareVisible = ref(false)
const shareOptions: ('wechat' | 'timeline' | 'link')[] = ['wechat', 'timeline', 'link']
const payload = {
title: '发现 / Discover',
url: 'https://example.com/discover',
path: '/pages/discover/index'
}
const make = (id: number) => ({
id,
title: '卡片 / Card ' + id,
src: banners[id % 2],
ratio: id % 2 ? 1.5 : 1,
failed: id === 2,
text: '完整长文本 / Full text. '.repeat(12)
})
const cards = ref([make(1), make(2), make(3)])
const estimateHeight = (item: unknown, context: { columnWidth: number; index: number }) =>
context.columnWidth / (item as { ratio: number }).ratio + 130
const append = () => {
const next = cards.value.length + 1
cards.value.push(make(next), make(next + 1))
lastEvent.value = '追加两项 / Two appended'
}
const filter = () => {
listKey.value++
cards.value = [make(1), make(2)]
lastEvent.value = '重新估高 / Layout reset'
}
</script>
Props
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
items | unknown[] | [] | PC Masonry 同名 |
columns | number | 2 | PC 同名 |
gap | number | [number, number] | 8 | 列间距、行间距,px |
itemKey | string | ((item: unknown, index: number) => string | number) | — | |
listKey | string | number | — | 列表版本:筛选条件、分类切换后换一个值,全部重新分列、卡片重建 |
estimateHeight | (item: unknown, context: WaterfallEstimateContext) => number | — | 估高函数,单位随意但要一致;不传时每项按同样高度(退化成轮流放) |
layout | WaterfallLayout | 'estimate' | 布局方式;预留 'measure',首期传了警告并按 estimate |
根节点类名和样式使用 customClass、customStyle。尺寸数字按 px,时长除明确说明外按 ms。
Events
| 事件 | 参数 |
|---|---|
layout | (columnHeights: number[]) |
受控状态由父级回写更新事件后改变。多词事件使用短横线。禁用操作、取消及过期异步结果不提交成功变更。
Slots
| 插槽 | 作用域 |
|---|---|
item | { item, index, column } |
empty | — |
方法
| 方法 | 签名 | 说明 |
|---|---|---|
relayout | (): void | 重新估高并排布全部数据 |
类型
WaterfallLayout、WaterfallEstimateContext、WaterfallProps、WaterfallEmits、WaterfallExpose.
平台说明
首期只支持 estimate。追加时同一 listKey 和键前缀保留旧列;筛选、替换或重新排序请更换 listKey,修改已有项高度或估高规则后调用 relayout。估高函数只在分列时读取,内联函数换引用不会自动重排;函数收到实际列宽,窗口宽度变化自动重新估高;错误或非法高度使用默认值。
设备冒烟清单.