Skip to content

Commit 940628b

Browse files
author
share121
committed
perf: 优化虚拟滚动性能
1 parent a43ffbd commit 940628b

File tree

1 file changed

+23
-24
lines changed

1 file changed

+23
-24
lines changed

src/components/HomePage.vue

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,22 @@
5252
size="small"
5353
/>
5454
</header>
55-
<TransitionGroup
56-
name="list"
57-
tag="main"
58-
ref="main"
59-
@before-leave="onBeforeLeave"
60-
>
61-
<template v-for="[index, item] in showList.entries()" :key="item.filePath">
62-
<div class="download-item" :style="{ height: itemSize[index] + 'px' }">
55+
<main ref="main">
56+
<TransitionGroup
57+
name="list"
58+
tag="div"
59+
:style="{ height: itemHeight + 'px' }"
60+
style="position: relative"
61+
@before-leave="onBeforeLeave"
62+
>
63+
<template
64+
v-for="[index, item] in showList.entries()"
65+
:key="item.filePath"
66+
>
6367
<DownloadItem
6468
v-if="itemVisible[index]"
69+
class="download-item"
70+
:style="{ top: itemVisibleRange[index].start + 'px' }"
6571
:downloaded="item.downloaded"
6672
:elapsed-ms="item.elapsedMs"
6773
:file-name="item.fileName"
@@ -78,9 +84,9 @@
7884
@detail="showDetail(item.filePath)"
7985
@toggle-open="item.opened = !item.opened"
8086
/>
81-
</div>
82-
</template>
83-
</TransitionGroup>
87+
</template>
88+
</TransitionGroup>
89+
</main>
8490
<UpdatePage v-model:visible="updatePageVisible" />
8591
<DetailPage v-model:visible="detailPageVisible" :file-path="detailItem" />
8692
</template>
@@ -109,10 +115,8 @@ const showList = computed(() =>
109115
store.list.filter(e => showTypes.value.includes(e.status)),
110116
)
111117
const itemSize = computed(() =>
112-
showList.value.map((item, i, arr) => {
113-
let height = 164
114-
if (i !== 0) height += 8 // margin-top
115-
if (i === arr.length - 1) height += 8 // margin-bottom
118+
showList.value.map(item => {
119+
let height = 172
116120
if (item.fileSize && item.readProgress.length) {
117121
height += 8 // footer gap
118122
if (item.opened) height += item.readProgress.length * 12
@@ -121,6 +125,7 @@ const itemSize = computed(() =>
121125
return height
122126
}),
123127
)
128+
const itemHeight = computed(() => itemSize.value.reduce((a, b) => a + b, 0))
124129
const itemVisibleRange = computed(() => {
125130
let current = 0
126131
return itemSize.value.map(size => {
@@ -170,14 +175,8 @@ main {
170175
overflow: hidden auto;
171176
}
172177
.download-item {
173-
box-sizing: border-box;
174-
padding: 8px 8px 0 8px;
175-
176-
&:first-child {
177-
padding-top: 0;
178-
}
179-
&:last-child {
180-
padding-bottom: 8px;
181-
}
178+
position: absolute;
179+
left: 8px;
180+
right: 8px;
182181
}
183182
</style>

0 commit comments

Comments
 (0)