Skip to main content

Observe Panel Resize

Use the observePanelResize option to attach a ResizeObserver to each panel element, so that Flicking automatically recalculates the layout when a panel's size changes.

Try clicking the "Expand" button inside a panel to see the difference between the two carousels.

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

// observePanelResize: false (default)
const _flick1 = new Flicking("#flick-default", {
  align: "center",
  observePanelResize: false
});

// observePanelResize: true
const _flick2 = new Flicking("#flick-observe", {
  align: "center",
  observePanelResize: true
});

// Toggle panel width on button click
document.querySelectorAll(".expand-btn").forEach(btn => {
  btn.addEventListener("click", e => {
    e.stopPropagation();
    const panel = btn.closest(".flicking-panel");
    const isWide = panel.style.width === "70%";
    panel.style.width = isWide ? "40%" : "70%";
    btn.textContent = isWide ? "Expand" : "Shrink";
  });
});

Summary

Key Options

OptionTypeDefaultDescription
observePanelResizebooleanfalseDetect panel element size changes via ResizeObserver

Dependent Option

observePanelResize only works when useResizeObserver is true. Since the default value of useResizeObserver is true, it can be used without additional configuration.

Value Comparison

ValueBehaviorBest For
falseOnly detects viewport size changes (default)Typical carousels with fixed panel sizes
trueAlso detects per-panel size changes, auto-recalculatesDynamic content, image loading, accordion panels

Details

How observePanelResize Works

By default, Flicking's autoResize only detects size changes on the viewport (container) element. Even if the content inside a panel changes and the panel itself resizes, Flicking is unaware of it.

Setting observePanelResize: true attaches a ResizeObserver to each panel element. As soon as a panel's size changes, beforeResize / afterResize events are fired and the layout is recalculated.

const flicking = new Flicking("#el", {
observePanelResize: true
// useResizeObserver: true (default, can be omitted)
});

Use Cases

When should you use this?
  • Panels with images: When panels grow to their actual size after image load completes
  • Accordion/toggle panels: When panel height changes due to user interaction
  • Dynamic content: When content is added to panels after an API response, changing their size
  • With adaptive: true: When viewport height adapts to the active panel height and panel content changes dynamically
Performance Consideration

observePanelResize: true attaches a ResizeObserver to every panel, which can introduce some overhead when there are many panels. For typical carousels with fixed panel sizes, keep the default (false).

Difference from resizeOnContentsReady

resizeOnContentsReady is an option that waits once for image/media load completion during initialization. observePanelResize continuously monitors panel sizes even after initialization. Choose based on your use case as they serve different purposes.