📖 Scenario: You are building a Vue app that shows a very long list of items, like a contact list or product catalog. Showing all items at once can slow down the page. Virtual scrolling helps by only rendering the items visible on screen.
🎯 Goal: Create a Vue 3 component that uses virtual scrolling to efficiently display a large list of 1000 items. The component should only render the visible items based on scroll position.
📋 What You'll Learn
Create a list of 1000 items with unique names
Add a reactive variable for the height of each item
Calculate which items are visible based on scroll position
Render only the visible items inside a scrollable container
💡 Why This Matters
🌍 Real World
Virtual scrolling is used in apps that show very long lists, like contact lists, chat messages, or product catalogs. It helps keep the app fast and responsive.
💼 Career
Understanding virtual scrolling is important for frontend developers to build efficient user interfaces that handle large data sets without slowing down the browser.
Progress0 / 4 steps
1
Create the list of 1000 items
Create a Vue 3 component with <script setup>. Inside it, create a reactive variable called items that is an array of 1000 objects. Each object should have a name property with the value Item 1, Item 2, up to Item 1000.
Vue
Hint
Use ref from Vue to create a reactive array. Use Array.from with length 1000 and map the index to create names.
2
Add item height and container height
Add two constants inside <script setup>: itemHeight set to 30 (pixels), and containerHeight set to 300 (pixels). These will define the height of each item and the visible container height.
Vue
Hint
Just add two constants with the exact names and values.
3
Calculate visible items based on scroll
Add a reactive variable scrollTop initialized to 0. Then create a computed property called visibleItems that calculates the start index as Math.floor(scrollTop / itemHeight) and the end index as start + Math.ceil(containerHeight / itemHeight). Return the slice of items.value from start to end.
Vue
Hint
Use ref for scrollTop and computed for visibleItems. Calculate start and end indexes carefully.
4
Render visible items in a scrollable container
In the template, create a <div> with style to set height: containerHeight + 'px', overflow-y: auto, and position: relative. Add a @scroll event handler that updates scrollTop.value with the event target's scrollTop. Inside this container, add another <div> with style setting height: items.length * itemHeight + 'px' and position: relative. Then use v-for on visibleItems to render each item in a <div> with style setting position: absolute, top: (index + Math.floor(scrollTop.value / itemHeight)) * itemHeight + 'px', height: itemHeight + 'px', and width: 100%. Display the item name inside each div.
Vue
Hint
Use a scrollable container div with fixed height and overflow-y auto. Update scrollTop on scroll. Render visible items absolutely positioned inside a tall inner div.
Practice
(1/5)
1. What is the main purpose of virtual scrolling in Vue applications when displaying large lists?
easy
A. To sort the list items automatically
B. To render only visible items to improve performance
C. To disable scrolling for better user control
D. To load all items at once for faster access
Solution
Step 1: Understand virtual scrolling concept
Virtual scrolling renders only the visible portion of a large list instead of all items at once.
Step 2: Identify the benefit in Vue apps
This reduces rendering load and improves performance when dealing with many items.
Final Answer:
To render only visible items to improve performance -> Option B
Quick Check:
Virtual scrolling = Render visible items only [OK]
Hint: Virtual scrolling shows visible items only to boost speed [OK]
Common Mistakes:
Thinking it loads all items at once
Believing it disables scrolling
Assuming it sorts items automatically
2. Which Vue template snippet correctly sets up a container for virtual scrolling with fixed height and overflow?
easy
A.
B.
C.
D.
Solution
Step 1: Identify container style for virtual scrolling
The container must have a fixed height and vertical scrolling enabled (overflow-y: auto).
Step 2: Match the correct style snippet
<div style="height: 300px; overflow-y: auto;"></div> sets height to 300px and overflow-y to auto, enabling vertical scroll inside fixed height.
Final Answer:
<div style="height: 300px; overflow-y: auto;"></div> -> Option A
D. Using Math.ceil causes skipping the first visible item
Solution
Step 1: Analyze use of Math.ceil for startIndex
Math.ceil rounds up, so if scrollTop/itemHeight is exactly 4, startIndex becomes 4, but if 3.1 it becomes 4, skipping some items.
Step 2: Understand impact on visible items
This causes the first partially visible item to be skipped, leading to missing items during scroll.
Final Answer:
Using Math.ceil causes skipping the first visible item -> Option D
Quick Check:
Math.ceil skips partially visible items [OK]
Hint: Use Math.floor, not Math.ceil, for start index [OK]
Common Mistakes:
Confusing slice with splice
Ignoring reactive state importance
Miscalculating endIndex
5. You want to implement virtual scrolling for a list of 10,000 items in Vue 3 using the Composition API. Which approach best ensures smooth scrolling and correct item rendering?
hard
A. Render all 10,000 items inside a scrollable div and rely on browser optimization
B. Use v-if to render only the first 100 items regardless of scroll position
C. Use a fixed height container with overflow-y:auto, calculate visible items on scroll, and absolutely position items inside a tall wrapper
D. Use a dynamic height container that grows as user scrolls down
Solution
Step 1: Understand virtual scrolling requirements
Virtual scrolling needs a fixed height container with scroll enabled and dynamic rendering of visible items.
Step 2: Identify correct rendering technique
Absolutely positioning items inside a tall wrapper simulates full list height while rendering only visible items improves performance.
Step 3: Evaluate other options
Rendering all items or ignoring scroll position causes lag; dynamic height container breaks scroll behavior.
Final Answer:
Use a fixed height container with overflow-y:auto, calculate visible items on scroll, and absolutely position items inside a tall wrapper -> Option C
Quick Check:
Fixed container + dynamic visible items + absolute positioning = Use a fixed height container with overflow-y:auto, calculate visible items on scroll, and absolutely position items inside a tall wrapper [OK]