LazyRender 延迟渲染
内容接近视口或指定滚动区域时才挂载。渲染后始终保留,离开视口不会卸载或丢失内部状态。
引入
ts
import { HuiLazyRender } from '@nscode/handy-ui'
import type { LazyRenderProps, LazyRenderEmits, LazyRenderExpose } from '@nscode/handy-ui'
示例
滚动容器中的二十块内容
每块内容的默认插槽只在进入观察范围后执行。下面显示首次渲染时刻,便于查看哪些内容已经挂载;离开再回来时,时间不会改变。
vue
<script setup lang="ts">
import { defineComponent, h, ref } from 'vue'
import { HuiLazyRender } from '@nscode/handy-ui'
const container = ref<HTMLElement | null>(null)
const rendered = ref(0)
const Block = defineComponent({
props: { index: { type: Number, required: true } },
setup(props) {
const time = new Date().toLocaleTimeString('zh-CN', { hour12: false })
return () => h('div', { class: 'block' }, `第 ${props.index} 块,渲染于 ${time}`)
}
})
</script>
<template>
<p>已渲染 {{ rendered }} / 20 块。向下滚动查看:</p>
<div ref="container" class="scroll-area">
<HuiLazyRender
v-for="index in 20"
:key="index"
:root="() => container"
:height="180"
root-margin="0px"
@render="rendered++"
>
<Block :index="index" />
<template #placeholder
><div class="placeholder">第 {{ index }} 块尚未渲染</div></template
>
</HuiLazyRender>
</div>
</template>
<style scoped>
.scroll-area {
height: 320px;
overflow: auto;
border: var(--hui-border-width) solid var(--hui-border-color);
}
.block,
.placeholder {
min-height: 180px;
display: flex;
align-items: center;
justify-content: center;
box-sizing: border-box;
padding: var(--hui-spacing-4);
border-bottom: var(--hui-border-width) solid var(--hui-border-color);
}
.block {
background: var(--hui-color-primary-bg);
}
.placeholder {
color: var(--hui-text-color-secondary);
background: var(--hui-fill-color-light);
}
</style>
骨架占位与提前渲染
rootMargin 默认 '200px',内容距离滚动区域边缘还有 200px 时开始渲染。传 '0px' 则等到实际进入;占位高度尽量接近真实内容,以减少滚动跳动。
vue
<script setup lang="ts">
import { ref } from 'vue'
import { HuiLazyRender, HuiSkeleton, HuiSpace } from '@nscode/handy-ui'
const container = ref<HTMLElement | null>(null)
const loaded = ref(false)
</script>
<template>
<HuiSpace direction="vertical" fill>
<span>{{ loaded ? '内容已渲染' : '向下滚动,提前 80px 开始渲染' }}</span>
<div ref="container" class="scroll-area">
<div class="spacer">向下滚动</div>
<HuiLazyRender
:root="() => container"
:height="180"
root-margin="80px 0px"
@render="loaded = true"
>
<div class="content">这里可以放图表、列表或其他较重的内容。</div>
<template #placeholder
><div class="skeleton"><HuiSkeleton :avatar="false" :rows="4" /></div
></template>
</HuiLazyRender>
</div>
</HuiSpace>
</template>
<style scoped>
.scroll-area {
height: 260px;
overflow: auto;
border: var(--hui-border-width) solid var(--hui-border-color);
}
.spacer {
height: 420px;
padding: var(--hui-spacing-3);
box-sizing: border-box;
color: var(--hui-text-color-secondary);
}
.content,
.skeleton {
min-height: 180px;
box-sizing: border-box;
padding: var(--hui-spacing-4);
}
.content {
background: var(--hui-color-primary-bg);
}
</style>
立即渲染与打印前准备
调用 render() 不等待相交;已经渲染过时再次调用不重复触发事件。打印前先渲染所有需要输出的块,再等待一次 nextTick()。
vue
<script setup lang="ts">
import { nextTick, ref } from 'vue'
import { HuiButton, HuiLazyRender, HuiMessage, HuiSpace } from '@nscode/handy-ui'
const container = ref<HTMLElement | null>(null)
const lazy = ref<InstanceType<typeof HuiLazyRender>>()
const count = ref(0)
const prepare = async () => {
lazy.value?.render()
await nextTick()
HuiMessage.success('内容已挂载,可以继续打印或导出')
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace wrap
><HuiButton @click="lazy?.render()">render()</HuiButton
><HuiButton @click="prepare">准备打印内容</HuiButton
><span>render 事件 {{ count }} 次</span></HuiSpace
>
<div ref="container" class="scroll-area">
<div class="spacer">下方内容尚未进入视口,也可从外面立即渲染。</div>
<HuiLazyRender
ref="lazy"
:root="() => container"
root-margin="0px"
:height="160"
@render="count++"
>
<div class="content">已挂载的打印内容。</div>
<template #placeholder><span>等待进入区域或调用方法</span></template>
</HuiLazyRender>
</div>
</HuiSpace>
</template>
<style scoped>
.scroll-area {
height: 240px;
overflow: auto;
border: var(--hui-border-width) solid var(--hui-border-color);
}
.spacer {
height: 400px;
padding: var(--hui-spacing-3);
box-sizing: border-box;
}
.content {
min-height: 160px;
padding: var(--hui-spacing-4);
box-sizing: border-box;
background: var(--hui-color-primary-bg);
}
</style>
选择器、元素与函数容器
root 支持选择器、HTML 元素和返回元素的函数。模板中同层创建的滚动容器适合使用函数,组件挂载后再解析。未渲染时切换容器或 rootMargin 会重建观察;已渲染内容保持不变。
vue
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiLazyRender, HuiSpace } from '@nscode/handy-ui'
const container = ref<HTMLElement | null>(null)
const mode = ref<'function' | 'element' | 'selector'>('function')
const root = () =>
mode.value === 'selector'
? '#lazy-render-root-demo'
: mode.value === 'element'
? container.value
: () => container.value
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace wrap
><HuiButton
v-for="value in ['function', 'element', 'selector']"
:key="value"
@click="mode = value as typeof mode"
>{{ value }}</HuiButton
><span>当前:{{ mode }}</span></HuiSpace
>
<div id="lazy-render-root-demo" ref="container" class="scroll-area">
<div class="spacer">向下滚动,或先切换 root 的传入形式。</div>
<HuiLazyRender :root="root() ?? undefined" :height="120" root-margin="0px"
><div class="content">三种容器传入方式都指向这里。</div>
<template #placeholder>等待渲染</template></HuiLazyRender
>
</div>
</HuiSpace>
</template>
<style scoped>
.scroll-area {
height: 220px;
overflow: auto;
border: var(--hui-border-width) solid var(--hui-border-color);
}
.spacer {
height: 360px;
padding: var(--hui-spacing-3);
box-sizing: border-box;
}
.content {
min-height: 120px;
padding: var(--hui-spacing-4);
background: var(--hui-color-primary-bg);
box-sizing: border-box;
}
</style>
标签与字符串占位高度
tag 只改变外层 HTML 元素;height 可以传 '12rem' 等 CSS 长度。未传 root 时观察浏览器视口;这块内容距离视口较近时会直接出现。
vue
<script setup lang="ts">
import { HuiLazyRender } from '@nscode/handy-ui'
</script>
<template>
<HuiLazyRender tag="section" height="12rem" root-margin="0px">
<section class="content">
<h3>文章区域</h3>
<p>首次接近浏览器视口后挂载,之后保持内容。</p>
</section>
<template #placeholder><div class="placeholder">文章占位</div></template>
</HuiLazyRender>
</template>
<style scoped>
.content,
.placeholder {
min-height: 12rem;
padding: var(--hui-spacing-4);
box-sizing: border-box;
background: var(--hui-fill-color-light);
}
</style>
Props
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| height | 渲染前占位最小高度,数字按 px | number | string | 200 |
| rootMargin | 交叉观察边距,正值提前开始渲染 | string | '200px' |
| root | 滚动容器;未传为视口 | string | HTMLElement | (() => HTMLElement | null | undefined) | — |
| tag | 外层 HTML 标签 | string | 'div' |
Events
| 事件 | 说明 | 参数 |
|---|---|---|
| render | 默认内容首次在客户端挂载完成后触发,仅一次 | — |
Slots
| 插槽 | 说明 | 参数 |
|---|---|---|
| default | 接近观察区域后才执行并挂载的内容 | — |
| placeholder | 渲染前的占位,可放骨架屏 | — |
方法
| 方法 | 说明 | 参数 |
|---|---|---|
| render | 立即请求渲染内容;重复调用不重复挂载或触发事件 | — |
类型
ts
interface LazyRenderProps {
height?: number | string
rootMargin?: string
root?: string | HTMLElement | (() => HTMLElement | null | undefined)
tag?: string
}
interface LazyRenderExpose {
render: () => void
}