<template> <main class="virtual-list-showcase"> <LazyNormalList /> <VirtualList /> </main></template><script lang="ts" setup>import LazyNormalList from './LazyNormalList.vue'import VirtualList from './VirtualList.vue'</script><style lang="less" scoped>.virtual-list-showcase { display: grid; gap: 24px; margin: 0 auto; max-width: 1040px; padding: 28px 20px 44px;}</style><template> <section class="lazy-demo"> <div v-if="!started" class="demo-intro"> <div> <h3>普通列表压力演示</h3> <p>默认不自动渲染,避免打开文章时一次性创建大量 DOM。</p> </div> <div class="demo-actions"> <button type="button" @click="start(1000)">渲染 1,000 行</button> <button type="button" class="danger" @click="start(10000)">渲染 10,000 行</button> </div> </div> <NormalList v-else :total="total" /> </section></template><script lang="ts" setup>import { ref } from 'vue'import NormalList from './NormalList.vue'const started = ref(false)const total = ref(1000)const start = (count: number) => { total.value = count started.value = true}</script><style lang="less" scoped>.lazy-demo { margin: 24px 0;}.demo-intro { display: flex; align-items: center; justify-content: space-between; gap: 16px; border: 1px solid #e4e6eb; border-radius: 8px; background: #fff; padding: 20px; box-shadow: 0 2px 12px rgb(29 33 41 / 4%);}.demo-intro h3,.demo-intro p { margin: 0;}.demo-intro h3 { color: #1d2129; font-size: 20px;}.demo-intro p { margin-top: 6px; color: #8a919f; font-size: 14px;}.demo-actions { display: flex; flex-wrap: wrap; gap: 10px; justify-content: flex-end;}.demo-actions button { border: 1px solid #e4e6eb; border-radius: 8px; background: #f7f8fa; color: #252933; cursor: pointer; font: inherit; font-size: 14px; padding: 10px 14px; transition: border-color 0.2s ease, background 0.2s ease, color 0.2s ease;}.demo-actions button:hover { border-color: #1e80ff; background: rgb(30 128 255 / 8%); color: #1e80ff;}.demo-actions .danger:hover { border-color: #f53f3f; background: rgb(245 63 63 / 8%); color: #f53f3f;}@media (max-width: 640px) { .demo-intro { align-items: flex-start; flex-direction: column; } .demo-actions { justify-content: flex-start; width: 100%; }}</style><template> <section class="list-card"> <header class="list-header"> <div> <h2>普通列表</h2> <p>一次性渲染完整数据</p> </div> <span>{{ listData.length }} 个节点</span> </header> <div class="list-view" :style="{ height: `${viewHeight}px` }"> <ul class="list-body"> <li v-for="item in listData" :key="item.id" class="list-item"> <strong>#{{ item.id }}</strong> <span>{{ item.title }}</span> <em>{{ item.status }}</em> </li> </ul> </div> </section></template><script lang="ts" setup>import { computed } from 'vue'interface ListItem { id: number title: string status: string}const props = withDefaults( defineProps<{ total?: number viewHeight?: number }>(), { total: 10000, viewHeight: 520, },)const viewHeight = computed(() => props.viewHeight)const listData = computed<ListItem[]>(() => Array.from({ length: props.total }, (_, index) => ({ id: index + 1, title: `订单渲染任务 ${index + 1}`, status: index % 3 === 0 ? '待处理' : index % 3 === 1 ? '执行中' : '已完成', })),)</script><style lang="less" scoped>.list-card { border: 1px solid #e4e6eb; border-radius: 8px; background: #fff; padding: 20px; box-shadow: 0 2px 12px rgb(29 33 41 / 4%);}.list-header { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; margin-bottom: 16px;}.list-header h2,.list-header p { margin: 0;}.list-header h2 { font-size: 22px; font-weight: 700;}.list-header p { margin-top: 6px; color: #8a919f; font-size: 13px;}.list-header span { color: #f53f3f; font-size: 14px;}.list-view { overflow: auto; border: 1px solid #e4e6eb; border-radius: 8px; background: #f7f8fa;}.list-body { margin: 0; padding: 0 8px; list-style: none;}.list-item { display: grid; grid-template-columns: 90px 1fr 82px; align-items: center; box-sizing: border-box; height: 56px; margin-bottom: 8px; border: 1px solid #e4e6eb; border-radius: 8px; background: #fff; padding: 0 16px;}.list-item strong { color: #1d2129;}.list-item span { min-width: 0; overflow: hidden; color: #515767; text-overflow: ellipsis; white-space: nowrap;}.list-item em { justify-self: end; color: #00b453; font-style: normal;}</style><template> <section class="list-card"> <header class="list-header"> <div> <h2>虚拟列表</h2> <p>只渲染视口附近数据</p> </div> <span>{{ showData.length }} 个节点</span> </header> <div class="list-view" :style="{ height: `${viewHeight}px` }" @scroll="onScroll"> <div class="list-space" :style="{ height: `${fullHeight}px` }"> <ul class="list-body" :style="{ transform: `translateY(${moveY}px)` }"> <li v-for="item in showData" :key="item.id" class="list-item"> <strong>#{{ item.id }}</strong> <span>{{ item.title }}</span> <em>{{ item.status }}</em> </li> </ul> </div> </div> </section></template><script lang="ts" setup>import { computed, ref } from 'vue'interface ListItem { id: number title: string status: string}const props = withDefaults( defineProps<{ total?: number rowHeight?: number viewHeight?: number buffer?: number }>(), { total: 10000, rowHeight: 64, viewHeight: 520, buffer: 6, },)const scrollTop = ref(0)const viewHeight = computed(() => props.viewHeight)const listData = computed<ListItem[]>(() => Array.from({ length: props.total }, (_, index) => ({ id: index + 1, title: `订单渲染任务 ${index + 1}`, status: index % 3 === 0 ? '待处理' : index % 3 === 1 ? '执行中' : '已完成', })),)const fullHeight = computed(() => props.total * props.rowHeight)const start = computed(() => Math.max(Math.floor(scrollTop.value / props.rowHeight) - props.buffer, 0),)const showCount = computed(() => Math.ceil(props.viewHeight / props.rowHeight))const end = computed(() => Math.min(showCount.value + start.value + props.buffer * 2, listData.value.length),)const showData = computed(() => listData.value.slice(start.value, end.value))const moveY = computed(() => start.value * props.rowHeight)const onScroll = (e: Event) => { scrollTop.value = (e.target as HTMLElement).scrollTop}</script><style lang="less" scoped>.list-card { border: 1px solid #e4e6eb; border-radius: 8px; background: #fff; padding: 20px; box-shadow: 0 2px 12px rgb(29 33 41 / 4%);}.list-header { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; margin-bottom: 16px;}.list-header h2,.list-header p { margin: 0;}.list-header h2 { font-size: 22px; font-weight: 700;}.list-header p { margin-top: 6px; color: #8a919f; font-size: 13px;}.list-header span { color: #1e80ff; font-size: 14px;}.list-view { overflow: auto; border: 1px solid #e4e6eb; border-radius: 8px; background: #f7f8fa;}.list-space { position: relative;}.list-body { position: absolute; top: 0; right: 0; left: 0; margin: 0; padding: 0 8px; list-style: none; will-change: transform;}.list-item { display: grid; grid-template-columns: 90px 1fr 82px; align-items: center; box-sizing: border-box; height: 56px; margin-bottom: 8px; border: 1px solid #e4e6eb; border-radius: 8px; background: #fff; padding: 0 16px;}.list-item strong { color: #1d2129;}.list-item span { min-width: 0; overflow: hidden; color: #515767; text-overflow: ellipsis; white-space: nowrap;}.list-item em { justify-self: end; color: #00b453; font-style: normal;}</style><template> <main class="list-page"> <section class="test-shell"> <NormalList /> <!-- <VirtualList /> --> </section> </main></template><script lang="ts" setup>import NormalList from './NormalList.vue'// import VirtualList from './VirtualList.vue'</script><style lang="less" scoped>.list-page { padding: 32px; color: #172033;}.test-shell { max-width: 1180px; margin: 0 auto;}</style>