Virtual Scroll
Use the virtual option to virtually render a large number of panels and significantly reduce memory usage.
- JavaScript
- React
- Vue@3
Summary
Key Options
| Option | Type | Default | Description |
|---|---|---|---|
virtual | VirtualOptions | null | null | Virtual rendering settings |
panelsPerView | number | -1 | Number of panels visible in the viewport (required when using virtual) |
VirtualOptions Properties
| Property | Type | Default | Description |
|---|---|---|---|
renderPanel | (panel, index) => string | - | Function that returns panel innerHTML |
initialPanelCount | number | -1 | Initial virtual panel count |
cache | boolean | false | Whether to cache rendering results |
panelClass | string | "flicking-panel" | Panel element class name |
Mode Comparison
| Mode | DOM Elements with 1000 panels | Memory Usage | Initial Rendering |
|---|---|---|---|
| Normal mode | 1000 | High | Slow |
| Virtual mode | ~4 (panelsPerView + 1) | Low | Fast |
Details
Virtual Rendering Principle
Virtual mode keeps only the visible panels + buffer in the actual DOM:
// 1000 logical panels, only 4 actual DOM elements
const flicking = new Flicking("#el", {
panelsPerView: 3, // Required: number of visible panels
virtual: {
renderPanel: (panel, index) => `Panel ${index + 1}`,
initialPanelCount: 1000,
cache: true // Prevent re-rendering of the same panel
}
});
During scrolling, DOM elements are recycled and replaced with new panel content. This approach can handle 1,000, 10,000 or more panels with constant memory usage.
Required Setting: panelsPerView
To use the virtual option, you must set panelsPerView to a positive number.
If panelsPerView: -1 (auto), virtual is ignored.
// Correct setting
{ panelsPerView: 3, virtual: { ... } } // Works
// Incorrect setting
{ panelsPerView: -1, virtual: { ... } } // virtual is ignored
renderPanel Callback
renderPanel is called whenever a panel appears on screen and returns its innerHTML:
virtual: {
renderPanel: (panel, index) => {
// index: 0-based panel index
return `
<img src="/images/item-${index}.jpg" />
<span>Item ${index + 1}</span>
`;
},
initialPanelCount: 1000
}
cache Option
// cache: true (recommended for static content)
virtual: {
renderPanel: (panel, index) => `Panel ${index}`,
cache: true // Reuse previously rendered panel content
}
// cache: false (for dynamic content)
virtual: {
renderPanel: (panel, index) => `Panel ${index}: ${Date.now()}`,
cache: false // Re-render every time
}
VirtualManager Methods
You can add/remove virtual panels at runtime:
// Add panels
flicking.virtual.append(100); // Append 100 to the end
flicking.virtual.prepend(50); // Prepend 50 to the front
flicking.virtual.insert(10, 20); // Insert 20 at index 10
// Remove panels
flicking.virtual.remove(0, 10); // Remove 10 starting from index 0
Use Cases
Recommended:
- When there are many panels (50+)
- Image galleries, product lists, and other large datasets
- Infinite scroll implementation
- Mobile environments with memory constraints
Not necessary:
- When there are few panels (less than 10)
- When complex panel interactions are needed
- When panel content is highly dynamic
Notes
In React and Vue, renderPanel returns an HTML string. If you need component rendering, consider the renderOnlyVisible option instead.
renderPanel is called frequently during scrolling. Avoid complex computations or DOM manipulations, and leverage cache: true.
Related Links
Related Options
panelsPerView: Number of panels visible in the viewportrenderOnlyVisible: Render only visible panels in React/Vue
Related Classes
VirtualManager: Virtual panel manager
Related Demos
- Render Only Visible: Framework-optimized rendering
- Infinite Scroll: Infinite scroll implementation