Skip to content
Source
Sign inSign up
TanStack/virtual
Open History to see the last commit

MasonryVerticalVirtualizerVariable.vue

56 lines1.3 KB

1<template>
2 <div
3 ref="parentRef"
4 class="List"
5 style="height: 200px; width: 400px; overflow: auto"
6 >
7 <div
8 :style="{
9 height: `${totalSize}px`,
10 width: '100%',
11 position: 'relative',
12 }"
13 >
14 <div
15 v-for="virtualRow in virtualRows"
16 :key="virtualRow.index"
17 :class="virtualRow.index % 2 ? 'ListItemOdd' : 'ListItemEven'"
18 :style="{
19 position: 'absolute',
20 top: 0,
21 left: `${virtualRow.lane * 25}%`,
22 width: '25%',
23 height: `${rows[virtualRow.index]}px`,
24 transform: `translateY(${virtualRow.start}px)`,
25 }"
26 >
27 Row {{ virtualRow.index }}
28 </div>
29 </div>
30 </div>
31</template>
32​
33<script setup lang="ts">
34import { ref, computed, PropType } from 'vue'
35import { useVirtualizer } from '@tanstack/vue-virtual'
36​
37const props = defineProps({
38 rows: {
39 type: Array as PropType<number[]>,
40 default: () => [],
41 },
42})
43​
44const parentRef = ref<HTMLElement | null>(null)
45​
46const rowVirtualizer = useVirtualizer({
47 count: props.rows.length,
48 getScrollElement: () => parentRef.value,
49 estimateSize: (i) => props.rows[i],
50 overscan: 5,
51 lanes: 4,
52})
53​
54const virtualRows = computed(() => rowVirtualizer.value.getVirtualItems())
55const totalSize = computed(() => rowVirtualizer.value.getTotalSize())
56</script>