2026-02-13 22:10:03 +08:00
|
|
|
|
<!--
|
|
|
|
|
|
VirtualScrollingDemo.vue
|
|
|
|
|
|
虚拟滚动演示
|
|
|
|
|
|
-->
|
2026-01-18 23:59:25 +08:00
|
|
|
|
<script setup>
|
2026-02-13 22:10:03 +08:00
|
|
|
|
import { ref, computed } from 'vue'
|
2026-01-18 23:59:25 +08:00
|
|
|
|
|
|
|
|
|
|
const TOTAL_ITEMS = 10000
|
|
|
|
|
|
const ITEM_HEIGHT = 50
|
2026-02-13 22:10:03 +08:00
|
|
|
|
const CONTAINER_HEIGHT = 280
|
2026-01-18 23:59:25 +08:00
|
|
|
|
|
|
|
|
|
|
// Generate mock data
|
|
|
|
|
|
const items = Array.from({ length: TOTAL_ITEMS }, (_, i) => ({
|
|
|
|
|
|
id: i,
|
2026-02-13 22:10:03 +08:00
|
|
|
|
content: `Item #${i + 1} - 虚拟滚动列表项内容`
|
2026-01-18 23:59:25 +08:00
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
|
|
const scrollTop = ref(0)
|
|
|
|
|
|
const containerRef = ref(null)
|
|
|
|
|
|
|
|
|
|
|
|
// Virtual scrolling calculations
|
|
|
|
|
|
const startIndex = computed(() => Math.floor(scrollTop.value / ITEM_HEIGHT))
|
2026-02-01 23:42:12 +08:00
|
|
|
|
const endIndex = computed(() =>
|
|
|
|
|
|
Math.min(
|
|
|
|
|
|
TOTAL_ITEMS,
|
|
|
|
|
|
startIndex.value + Math.ceil(CONTAINER_HEIGHT / ITEM_HEIGHT) + 2
|
|
|
|
|
|
)
|
|
|
|
|
|
)
|
2026-01-18 23:59:25 +08:00
|
|
|
|
const visibleItems = computed(() => {
|
2026-02-01 23:42:12 +08:00
|
|
|
|
return items.slice(startIndex.value, endIndex.value).map((item) => ({
|
2026-01-18 23:59:25 +08:00
|
|
|
|
...item,
|
|
|
|
|
|
top: item.id * ITEM_HEIGHT
|
|
|
|
|
|
}))
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
const totalHeight = TOTAL_ITEMS * ITEM_HEIGHT
|
|
|
|
|
|
|
|
|
|
|
|
const onScroll = (e) => {
|
|
|
|
|
|
scrollTop.value = e.target.scrollTop
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Stats
|
|
|
|
|
|
const renderedCount = computed(() => visibleItems.value.length)
|
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
|
|
<template>
|
|
|
|
|
|
<div class="demo-container">
|
2026-02-13 22:10:03 +08:00
|
|
|
|
<div class="demo-header">
|
|
|
|
|
|
<span class="icon">📜</span>
|
|
|
|
|
|
<span class="title">虚拟滚动</span>
|
|
|
|
|
|
<span class="subtitle">只渲染可见区域的列表项</span>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
2026-01-18 23:59:25 +08:00
|
|
|
|
<div class="controls">
|
|
|
|
|
|
<div class="stat-box">
|
2026-02-18 17:38:10 +08:00
|
|
|
|
<div class="stat-label">
|
|
|
|
|
|
总数据量
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div class="stat-value">
|
|
|
|
|
|
{{ TOTAL_ITEMS.toLocaleString() }}
|
|
|
|
|
|
</div>
|
2026-01-18 23:59:25 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
<div class="stat-box highlight">
|
2026-02-18 17:38:10 +08:00
|
|
|
|
<div class="stat-label">
|
|
|
|
|
|
实际渲染
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div class="stat-value">
|
|
|
|
|
|
{{ renderedCount }}
|
|
|
|
|
|
</div>
|
2026-01-18 23:59:25 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
<div class="stat-box">
|
2026-02-18 17:38:10 +08:00
|
|
|
|
<div class="stat-label">
|
|
|
|
|
|
节省内存
|
|
|
|
|
|
</div>
|
2026-02-01 23:42:12 +08:00
|
|
|
|
<div class="stat-value">
|
|
|
|
|
|
~{{ ((1 - renderedCount / TOTAL_ITEMS) * 100).toFixed(1) }}%
|
|
|
|
|
|
</div>
|
2026-01-18 23:59:25 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
2026-02-01 23:42:12 +08:00
|
|
|
|
<div
|
2026-01-18 23:59:25 +08:00
|
|
|
|
ref="containerRef"
|
2026-02-18 17:38:10 +08:00
|
|
|
|
class="scroll-container"
|
2026-01-18 23:59:25 +08:00
|
|
|
|
:style="{ height: CONTAINER_HEIGHT + 'px' }"
|
2026-02-18 17:38:10 +08:00
|
|
|
|
@scroll="onScroll"
|
2026-01-18 23:59:25 +08:00
|
|
|
|
>
|
2026-02-18 17:38:10 +08:00
|
|
|
|
<div
|
|
|
|
|
|
class="scroll-phantom"
|
|
|
|
|
|
:style="{ height: totalHeight + 'px' }"
|
|
|
|
|
|
/>
|
2026-01-18 23:59:25 +08:00
|
|
|
|
<div class="visible-list">
|
2026-02-01 23:42:12 +08:00
|
|
|
|
<div
|
|
|
|
|
|
v-for="item in visibleItems"
|
2026-01-18 23:59:25 +08:00
|
|
|
|
:key="item.id"
|
|
|
|
|
|
class="list-item"
|
2026-02-01 23:42:12 +08:00
|
|
|
|
:style="{
|
|
|
|
|
|
transform: `translateY(${item.top}px)`,
|
|
|
|
|
|
height: ITEM_HEIGHT + 'px'
|
|
|
|
|
|
}"
|
2026-01-18 23:59:25 +08:00
|
|
|
|
>
|
|
|
|
|
|
<span class="item-index">{{ item.id + 1 }}</span>
|
|
|
|
|
|
<span class="item-content">{{ item.content }}</span>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
2026-02-13 22:10:03 +08:00
|
|
|
|
<div class="info-box">
|
|
|
|
|
|
<span class="icon">💡</span>
|
|
|
|
|
|
<strong>工作原理:</strong>不渲染全部 {{ TOTAL_ITEMS }} 项,只渲染视口中可见的项(加上少量缓冲)。滚动时计算应该显示哪些项,并使用绝对定位创建完整列表的错觉。性能从 O(n) 优化到 O(1)。
|
2026-01-18 23:59:25 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
|
|
<style scoped>
|
|
|
|
|
|
.demo-container {
|
|
|
|
|
|
border: 1px solid var(--vp-c-divider);
|
2026-02-14 20:23:34 +08:00
|
|
|
|
border-radius: 6px;
|
2026-02-13 22:10:03 +08:00
|
|
|
|
background: var(--vp-c-bg-soft);
|
2026-02-14 20:23:34 +08:00
|
|
|
|
padding: 0.75rem;
|
|
|
|
|
|
margin: 0.5rem 0;
|
2026-01-18 23:59:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-13 22:10:03 +08:00
|
|
|
|
.demo-header {
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
align-items: center;
|
|
|
|
|
|
gap: 0.5rem;
|
|
|
|
|
|
margin-bottom: 0.75rem;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.demo-header .icon { font-size: 1.25rem; }
|
|
|
|
|
|
.demo-header .title { font-weight: bold; font-size: 1rem; }
|
|
|
|
|
|
.demo-header .subtitle { color: var(--vp-c-text-2); font-size: 0.85rem; margin-left: 0.5rem; }
|
|
|
|
|
|
|
2026-01-18 23:59:25 +08:00
|
|
|
|
.controls {
|
|
|
|
|
|
display: flex;
|
2026-02-13 22:10:03 +08:00
|
|
|
|
gap: 0.75rem;
|
|
|
|
|
|
margin-bottom: 0.75rem;
|
2026-01-18 23:59:25 +08:00
|
|
|
|
flex-wrap: wrap;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.stat-box {
|
2026-02-13 22:10:03 +08:00
|
|
|
|
background: var(--vp-c-bg);
|
|
|
|
|
|
padding: 0.6rem;
|
2026-01-18 23:59:25 +08:00
|
|
|
|
border-radius: 6px;
|
|
|
|
|
|
flex: 1;
|
2026-02-13 22:10:03 +08:00
|
|
|
|
min-width: 100px;
|
2026-01-18 23:59:25 +08:00
|
|
|
|
text-align: center;
|
|
|
|
|
|
border: 1px solid var(--vp-c-divider);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.stat-box.highlight {
|
|
|
|
|
|
border-color: var(--vp-c-brand);
|
2026-02-13 22:10:03 +08:00
|
|
|
|
background: var(--vp-c-brand-dimm);
|
2026-01-18 23:59:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.stat-label {
|
2026-02-13 22:10:03 +08:00
|
|
|
|
font-size: 0.7rem;
|
2026-01-18 23:59:25 +08:00
|
|
|
|
color: var(--vp-c-text-2);
|
2026-02-13 22:10:03 +08:00
|
|
|
|
margin-bottom: 0.25rem;
|
2026-01-18 23:59:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.stat-value {
|
2026-02-13 22:10:03 +08:00
|
|
|
|
font-size: 1.1rem;
|
2026-01-18 23:59:25 +08:00
|
|
|
|
font-weight: bold;
|
|
|
|
|
|
color: var(--vp-c-text-1);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.scroll-container {
|
2026-02-14 20:23:34 +08:00
|
|
|
|
|
2026-01-18 23:59:25 +08:00
|
|
|
|
position: relative;
|
2026-02-13 22:10:03 +08:00
|
|
|
|
background: var(--vp-c-bg);
|
2026-01-18 23:59:25 +08:00
|
|
|
|
border: 1px solid var(--vp-c-divider);
|
|
|
|
|
|
border-radius: 6px;
|
2026-02-13 22:10:03 +08:00
|
|
|
|
margin-bottom: 0.75rem;
|
2026-01-18 23:59:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.scroll-phantom {
|
|
|
|
|
|
position: absolute;
|
|
|
|
|
|
top: 0;
|
|
|
|
|
|
left: 0;
|
|
|
|
|
|
right: 0;
|
|
|
|
|
|
z-index: -1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.visible-list {
|
|
|
|
|
|
position: absolute;
|
|
|
|
|
|
top: 0;
|
|
|
|
|
|
left: 0;
|
|
|
|
|
|
right: 0;
|
|
|
|
|
|
height: 100%;
|
2026-02-13 22:10:03 +08:00
|
|
|
|
pointer-events: none;
|
2026-01-18 23:59:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.list-item {
|
|
|
|
|
|
position: absolute;
|
|
|
|
|
|
top: 0;
|
|
|
|
|
|
left: 0;
|
|
|
|
|
|
right: 0;
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
align-items: center;
|
2026-02-13 22:10:03 +08:00
|
|
|
|
padding: 0 1rem;
|
2026-01-18 23:59:25 +08:00
|
|
|
|
border-bottom: 1px solid var(--vp-c-divider);
|
|
|
|
|
|
box-sizing: border-box;
|
2026-02-13 22:10:03 +08:00
|
|
|
|
background: var(--vp-c-bg);
|
2026-01-18 23:59:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.item-index {
|
|
|
|
|
|
font-weight: bold;
|
|
|
|
|
|
color: var(--vp-c-brand);
|
|
|
|
|
|
width: 50px;
|
|
|
|
|
|
flex-shrink: 0;
|
2026-02-13 22:10:03 +08:00
|
|
|
|
font-size: 0.85rem;
|
2026-01-18 23:59:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.item-content {
|
|
|
|
|
|
color: var(--vp-c-text-1);
|
|
|
|
|
|
white-space: nowrap;
|
|
|
|
|
|
overflow: hidden;
|
|
|
|
|
|
text-overflow: ellipsis;
|
2026-02-13 22:10:03 +08:00
|
|
|
|
font-size: 0.85rem;
|
2026-01-18 23:59:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-13 22:10:03 +08:00
|
|
|
|
.info-box {
|
|
|
|
|
|
background: var(--vp-c-bg-alt);
|
|
|
|
|
|
padding: 0.75rem;
|
|
|
|
|
|
border-radius: 6px;
|
|
|
|
|
|
font-size: 0.85rem;
|
2026-01-18 23:59:25 +08:00
|
|
|
|
color: var(--vp-c-text-2);
|
|
|
|
|
|
}
|
2026-02-13 22:10:03 +08:00
|
|
|
|
|
|
|
|
|
|
.info-box .icon { margin-right: 0.25rem; }
|
2026-01-18 23:59:25 +08:00
|
|
|
|
</style>
|