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
}