0
0
Vueframework~10 mins

Virtual scrolling for large lists in Vue - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Virtual scrolling for large lists
Start: Large list data
Calculate visible window size
Calculate start index based on scroll
Render only visible items
User scrolls
Update start index
Re-render visible items
Back to User scrolls
Virtual scrolling shows only the visible part of a large list, updating rendered items as the user scrolls.
Execution Sample
Vue
const visibleCount = 10;
const startIndex = ref(0);
const visibleItems = computed(() =>
  items.slice(startIndex.value, startIndex.value + visibleCount)
);

function onScroll(event) {
  startIndex.value = Math.floor(event.target.scrollTop / itemHeight);
}
This code calculates which items to show based on scroll position and renders only those items.
Execution Table
StepScrollTop (px)startIndexVisible Items Index RangeRendered Items Count
1000 - 910
25011 - 1010
315033 - 1210
445099 - 1810
59501919 - 2810
69951919 - 2810
710002020 - 2910
ExitScrollTop exceeds max scroll--Stop updating startIndex
💡 When scrollTop exceeds the maximum scroll height, no further updates to startIndex occur.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6After Step 7Final
scrollTop005015045095099510001000
startIndex0013919192020
visibleItems range0-90-91-103-129-1819-2819-2820-2920-29
renderedItems count101010101010101010
Key Moments - 3 Insights
Why do we only render a subset of items instead of the whole list?
Rendering all items slows down the app and uses too much memory. The execution_table shows we only render 10 items at a time, improving performance.
How is startIndex calculated from scrollTop?
startIndex is scrollTop divided by item height, rounded down. See execution_table rows where scrollTop 150 gives startIndex 3 (150/50=3).
What happens when scrollTop is near the bottom of the list?
startIndex stops increasing once the last items are visible, preventing out-of-range errors. The exit row shows no update beyond max scroll.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 3, what is the startIndex value?
A3
B9
C1
D0
💡 Hint
Check the 'startIndex' column in execution_table row for Step 3.
At which step does the visible items range first start at index 9?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Visible Items Index Range' column in execution_table.
If itemHeight doubled, how would startIndex change for scrollTop 150?
AstartIndex would be 3
BstartIndex would be 1
CstartIndex would be 6
DstartIndex would be 0
💡 Hint
startIndex = floor(scrollTop / itemHeight). Doubling itemHeight halves startIndex.
Concept Snapshot
Virtual scrolling shows only visible items of a large list.
Calculate startIndex = floor(scrollTop / itemHeight).
Render items from startIndex to startIndex + visibleCount.
Update startIndex on scroll to re-render visible items.
Improves performance by reducing DOM nodes.
Full Transcript
Virtual scrolling is a technique to efficiently display large lists by rendering only the items visible in the scroll window. The code calculates which items to show based on the scroll position and item height. As the user scrolls, the startIndex updates, and only the visible items are rendered. This reduces memory use and improves app speed. The execution table shows how scrollTop changes affect startIndex and which items are rendered. When scrolling near the bottom, updates stop to avoid errors. This method is common in Vue apps to handle big lists smoothly.