본문으로 건너뛰기

Parallax

Reactive API의 indexProgress를 사용하여 패널이 카메라 중앙에서 멀어질수록 내부 요소들이 서로 다른 속도로 움직이는 패럴랙스 효과를 만듭니다.

import Flicking, { connectFlickingReactiveAPI } from "@egjs/flicking";
import "@egjs/flicking/dist/flicking.css";
import "./styles.css";

const OFFSETS = [180, 160, 140, 120, 100];

const flicking = new Flicking("#flick");
const reactiveAPI = connectFlickingReactiveAPI(flicking);

const panels = document.querySelectorAll(".skeleton-panel");

const update = value => {
  panels.forEach((panel, index) => {
    const childProgress = index - value;
    const opacity = Math.min(Math.max(1 - Math.abs(childProgress), 0), 1);

    const bars = panel.querySelectorAll(".skeleton-bar");
    bars.forEach((bar, i) => {
      bar.style.transform = `translateX(${childProgress * OFFSETS[i]}px)`;
      bar.style.opacity = opacity;
    });
  });
};

// Apply initial state and subscribe to changes
update(reactiveAPI.indexProgress);
reactiveAPI.subscribe("indexProgress", update);

요약

주요 API

속성타입설명
indexProgressnumber소수점 패널 인덱스로 표현된 카메라 위치

효과 매핑

패널 위치translateX투명도(Opacity)
현재 (childProgress = 0)0px1.0
인접 (childProgress = ±1)±offset0.0

상세 설명

동작 원리

각 패널에는 스켈레톤 바 요소가 포함되어 있습니다. 각 패널의 childProgresspanelIndex - indexProgress로 계산됩니다. 이 값은 두 가지 시각 효과를 제어합니다:

  1. 수평 이동: 각 바는 childProgress * offset만큼 이동하며, 각 바마다 서로 다른 offset 값(100-180px)을 가지므로 층층이 쌓인 깊이감 효과가 생깁니다.
  2. 투명도: |childProgress| 값에 따라 현재 패널(1)에서 인접 패널(0)로 서서히 사라집니다.

바마다 다른 offset 값이 패럴랙스 특유의 시각 효과를 만들어냅니다: "카메라"에 가까운 요소가 먼 요소보다 더 빠르게 움직이는 것처럼 보입니다.

관련 옵션

  • moveType: "freeScroll": 자유 스크롤로 더 부드럽고 연속적인 패럴랙스 효과를 만듭니다.
  • 기본 moveType (snap): 패럴랙스 효과가 동작하지만 패널 위치에 스냅됩니다.

사용 시나리오

이런 경우에 사용하세요
  • 레이어 요소가 있는 카드 기반 콘텐츠
  • 스토리텔링 / 에디토리얼 캐러셀
  • 깊이감 효과가 있는 제품 쇼케이스

관련 링크

관련 API

관련 데모