Infinite Scroll
Implement infinite scroll using the needPanelThreshold option and the needPanel event.
- JavaScript
- React
- Vue@3
Summary
Key Options
| Option | Type | Default | Description |
|---|---|---|---|
needPanelThreshold | number | 0 | Threshold for triggering the needPanel event (px) |
Related Events
| Event | Description |
|---|---|
needPanel | Fired when empty space is visible at the viewport edge |
Threshold Comparison
| needPanelThreshold | Behavior |
|---|---|
0 (default) | Event fires only when the end is fully reached |
100 | Event fires 100px before the end (preloading) |
200 | Event fires 200px before the end |
Details
How It Works
- Scroll approaches the viewport edge
- When within the
needPanelThresholddistance, theneedPanelevent fires - Add new panels in the event handler
- Scrolling can continue
<Flicking
needPanelThreshold={100}
onNeedPanel={(e) => {
if (e.direction === "NEXT") {
// Append panels
setPanels(prev => [...prev, newPanel]);
}
}}
>
{panels.map(p => <Panel key={p.id} />)}
</Flicking>
needPanel Event
interface NeedPanelEvent {
direction: "PREV" | "NEXT";
}
- Use
directionto determine which direction needs panels "PREV": Panels needed at the front (prepend)"NEXT": Panels needed at the end (append)
Adding Panels in Vanilla JS
flicking.on("needPanel", (e) => {
if (e.direction === "NEXT") {
const newPanel = document.createElement("div");
newPanel.className = "flicking-panel";
newPanel.textContent = "New Panel";
flicking.append(newPanel);
}
});
Use Cases
When should you use needPanelThreshold?
Recommended:
- Infinite scroll implementation
- Pagination (load next page when reaching the end)
- Progressive loading of large datasets
Appropriate threshold values:
0: When the end must be exactly reached100~200: Typical preloading- Viewport width or more: Very aggressive preloading
Notes
Duplicate Event Firing
The needPanel event may fire again while panels are being added. Manage loading state to prevent duplicate requests.
const [isLoading, setIsLoading] = useState(false);
const handleNeedPanel = async (e) => {
if (isLoading) return; // Prevent duplicates
setIsLoading(true);
await loadMorePanels();
setIsLoading(false);
};
Related Links
Related Events
needPanel: Panel needed event
Related Methods
Related Demos
- Virtual Scroll: Virtual rendering of large numbers of panels