Skip to main content

Add / Remove

Dynamically add or remove panels using the prepend(), append(), and remove() API methods.

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

const COLORS = ["#e74c3c", "#3498db", "#2ecc71", "#f39c12", "#9b59b6"];
let counter = 5;

const camera = document.querySelector(".flicking-camera");
for (let i = 0; i < 5; i++) {
  const panel = document.createElement("div");
  panel.className = "flicking-panel";
  panel.style.background = COLORS[i % COLORS.length];
  panel.textContent = i;
  camera.appendChild(panel);
}

const flicking = new Flicking("#flick", {
  renderOnlyVisible: true,
  align: "prev",
  bound: true
});

const infoBar = document.querySelector(".info-bar");
const updateInfo = () => {
  infoBar.textContent = `Panel count: ${flicking.panelCount}`;
};

document.getElementById("btn-prepend").addEventListener("click", () => {
  flicking.prepend(
    `<div class="flicking-panel" style="background:${COLORS[counter % COLORS.length]}">${counter}</div>`
  );
  counter++;
  updateInfo();
});

document.getElementById("btn-append").addEventListener("click", () => {
  flicking.append(`<div class="flicking-panel" style="background:${COLORS[counter % COLORS.length]}">${counter}</div>`);
  counter++;
  updateInfo();
});

document.getElementById("btn-remove-first").addEventListener("click", () => {
  if (flicking.panelCount > 1) {
    flicking.remove(0);
    updateInfo();
  }
});

document.getElementById("btn-remove-last").addEventListener("click", () => {
  if (flicking.panelCount > 1) {
    flicking.remove(flicking.panelCount - 1);
    updateInfo();
  }
});

Summary

Key API Methods

MethodSignatureDescription
appendappend(element): Panel[]Add after the last panel
prependprepend(element): Panel[]Add before the first panel
removeremove(index, count?): Panel[]Remove panel at specified index

Behavior Comparison

MethodIndex ChangeBest For
prependExisting panel indices increase by +1Adding latest items to the front
appendExisting panel indices unchangedAdding new items to the end of a feed
removeIndices after removed panel decrease by -1Deleting items

Details

Framework-Specific Patterns

React / Vue: Manipulate the panel array through the framework's state management. Flicking detects state changes and automatically updates panels.

Vanilla JS: Directly call the prepend(), append(), remove() methods on the Flicking instance. You can pass an HTMLElement or an outerHTML string as the element parameter.

  • Relationship with renderOnlyVisible: When there are many panels, using renderOnlyVisible: true together maintains performance even with dynamic additions
  • Relationship with needPanelThreshold: Can be combined with the needPanel event to implement an infinite scroll pattern

Use Cases

When should you use this?
  • Social media feeds: prepend new posts to the front, append older posts to the back
  • E-commerce product lists: Remove/add panels based on filtering
  • Dashboard widgets: Add/remove widgets

Notes

Caution
  • When using prepend, all existing panel indices increase by 1. Be careful if you have logic that depends on indices.
  • If the currently visible panel is removed after a remove, it automatically moves to an adjacent panel.
  • In React/Vue, each panel must have a unique key to work properly.
  • Infinite Scroll: Automatic panel addition with the needPanel event
  • Lazy Load: Optimization pattern used with renderOnlyVisible