Skip to main content

Render Only Visible

Use the renderOnlyVisible option to keep only visible panels in the DOM, improving performance.

import Flicking from "@egjs/flicking";
import "@egjs/flicking/dist/flicking.css";
import "./styles.css";

const TOTAL = 20;
const COLORS = ["#3e8ed0", "#00d1b2", "#f14668", "#ffe08a", "#48c78e", "#9c27b0", "#ff5722"];

// Create panels
function createPanels(cameraSelector) {
  const camera = document.querySelector(cameraSelector);
  for (let i = 0; i < TOTAL; i++) {
    const panel = document.createElement("div");
    panel.className = "flicking-panel";
    panel.style.background = COLORS[i % COLORS.length];
    panel.textContent = `Panel ${i + 1}`;
    camera.appendChild(panel);
  }
}

createPanels("#flick-visible .flicking-camera");
createPanels("#flick-normal .flicking-camera");

const visibleCountEl = document.getElementById("visible-count");
const normalCountEl = document.getElementById("normal-count");

function updateCounts() {
  visibleCountEl.textContent = document.querySelectorAll("#flick-visible .flicking-camera > .flicking-panel").length;
  normalCountEl.textContent = document.querySelectorAll("#flick-normal .flicking-camera > .flicking-panel").length;
}

// renderOnlyVisible: true
const visibleFlicking = new Flicking("#flick-visible", {
  align: "prev",
  renderOnlyVisible: true
});

visibleFlicking.on("ready", updateCounts);
visibleFlicking.on("visibleChange", updateCounts);

// renderOnlyVisible: false (default)
const normalFlicking = new Flicking("#flick-normal", {
  align: "prev"
});

normalFlicking.on("ready", updateCounts);

Summary

Key Options

OptionTypeDefaultDescription
renderOnlyVisiblebooleanfalseKeep only visible panels in the DOM

Mode Comparison

SettingDOM Panel CountOn ScrollMemory
false (default)All panelsNo changeHigh
trueOnly visible panelsDOM add/remove occursLow

Details

How It Works

Setting renderOnlyVisible: true removes DOM elements of non-visible panels from the camera container.
When scrolling brings a new panel into view, it is added to the DOM, and panels that leave the viewport are removed from the DOM.

// Vanilla JS
const flicking = new Flicking("#flick", {
renderOnlyVisible: true
});
// React
<Flicking renderOnlyVisible={true}>
{panels.map(p => <Panel key={p.id} />)}
</Flicking>

Difference Between Vanilla JS and Frameworks

This option works in both Vanilla JS and frameworks.

EnvironmentBehaviorEffect
Vanilla JSDOM removal/addition via removeChild/appendChildReduced DOM node count
React / VueDOM removal/addition via framework render cycle + component skip renderingReduced DOM node count + reduced rendering cost

In frameworks, the component rendering cost itself is also reduced, resulting in a greater effect.

Difference from the virtual Option

FeaturerenderOnlyVisiblevirtual
DOM element countOnly visible panels (gradually added/removed)panelsPerView + buffer (fixed)
Applicable environmentAll environmentsAll environments
Panel contentNo restrictionsOnly innerHTML strings from renderPanel callback
Configuration complexitySimple (boolean)Complex (renderPanel callback required)

Use Cases

When should you use renderOnlyVisible?

Recommended:

  • When there are many panels and you want to reduce DOM nodes
  • When panel components are complex in frameworks
  • When you want to avoid the complexity of virtual

virtual is more suitable when:

  • There are very many panels (100+)
  • Memory usage needs to be minimized
  • Panel content is simple
  • virtual: Virtualization that limits DOM elements themselves