Carousel

Carousel는 같은 수준의 콘텐츠 그룹을 회전문 형태로 보여주며 공간을 아낄 수 있는 컴포넌트예요. 그림/카드 그룹에 흔히 쓰이죠.

출처: 문서

본문

언제 사용하나요

  • 같은 수준의 콘텐츠 그룹이 있을 때.
  • 콘텐츠 공간이 부족할 때, 회전문 형태로 공간을 아끼는 데 사용할 수 있어요.
  • 그림/카드 그룹에 흔히 사용돼요.

예제 (Examples)

기본 (Basic)

기본 사용법이에요.

import React from 'react';
import { Carousel } from 'antd';

const contentStyle: React.CSSProperties = {
  margin: 0,
  height: '160px',
  color: '#fff',
  lineHeight: '160px',
  textAlign: 'center',
  background: '#364d79',
};

const App: React.FC = () => {
  const onChange = (currentSlide: number) => {
    console.log(currentSlide);
  };

  return (
    <Carousel afterChange={onChange}>
      <div>
        <h3 style={contentStyle}>1</h3>
      </div>
      <div>
        <h3 style={contentStyle}>2</h3>
      </div>
      <div>
        <h3 style={contentStyle}>3</h3>
      </div>
      <div>
        <h3 style={contentStyle}>4</h3>
      </div>
    </Carousel>
  );
};

export default App;

위치 (Position)

4가지 위치 옵션이 있어요.

import React, { useState } from 'react';
import type { CarouselProps, RadioChangeEvent } from 'antd';
import { Carousel, Radio } from 'antd';

type DotPlacement = CarouselProps['dotPlacement'];

const contentStyle: React.CSSProperties = {
  margin: 0,
  height: '160px',
  color: '#fff',
  lineHeight: '160px',
  textAlign: 'center',
  background: '#364d79',
};

const App: React.FC = () => {
  const [dotPlacement, setDotPlacement] = useState<DotPlacement>('top');

  const handlePositionChange = ({ target: { value } }: RadioChangeEvent) => {
    setDotPlacement(value);
  };

  return (
    <>
      <Radio.Group onChange={handlePositionChange} value={dotPlacement} style={{ marginBottom: 8 }}>
        <Radio.Button value="top">Top</Radio.Button>
        <Radio.Button value="bottom">Bottom</Radio.Button>
        <Radio.Button value="start">Start</Radio.Button>
        <Radio.Button value="end">End</Radio.Button>
      </Radio.Group>
      <Carousel dotPlacement={dotPlacement}>
        <div>
          <h3 style={contentStyle}>1</h3>
        </div>
        <div>
          <h3 style={contentStyle}>2</h3>
        </div>
        <div>
          <h3 style={contentStyle}>3</h3>
        </div>
        <div>
          <h3 style={contentStyle}>4</h3>
        </div>
      </Carousel>
    </>
  );
};

export default App;

자동 스크롤 (Scroll automatically)

다음 카드/그림으로 스크롤되는 타이밍이에요.

import React from 'react';
import { Carousel } from 'antd';

const contentStyle: React.CSSProperties = {
  margin: 0,
  height: '160px',
  color: '#fff',
  lineHeight: '160px',
  textAlign: 'center',
  background: '#364d79',
};

const App: React.FC = () => (
  <Carousel autoplay>
    <div>
      <h3 style={contentStyle}>1</h3>
    </div>
    <div>
      <h3 style={contentStyle}>2</h3>
    </div>
    <div>
      <h3 style={contentStyle}>3</h3>
    </div>
    <div>
      <h3 style={contentStyle}>4</h3>
    </div>
  </Carousel>
);

export default App;

페이드 인 (Fade in)

슬라이드가 페이드로 전환돼요.

import React from 'react';
import { Carousel } from 'antd';

const contentStyle: React.CSSProperties = {
  margin: 0,
  height: '160px',
  color: '#fff',
  lineHeight: '160px',
  textAlign: 'center',
  background: '#364d79',
};

const App: React.FC = () => (
  <Carousel effect="fade">
    <div>
      <h3 style={contentStyle}>1</h3>
    </div>
    <div>
      <h3 style={contentStyle}>2</h3>
    </div>
    <div>
      <h3 style={contentStyle}>3</h3>
    </div>
    <div>
      <h3 style={contentStyle}>4</h3>
    </div>
  </Carousel>
);

export default App;

전환 화살표 (Arrows for switching)

전환용 화살표를 표시해요.

import React from 'react';
import { Carousel } from 'antd';

const contentStyle: React.CSSProperties = {
  margin: 0,
  height: '160px',
  color: '#fff',
  lineHeight: '160px',
  textAlign: 'center',
  background: '#364d79',
};

const App: React.FC = () => (
  <>
    <Carousel arrows infinite={false}>
      <div>
        <h3 style={contentStyle}>1</h3>
      </div>
      <div>
        <h3 style={contentStyle}>2</h3>
      </div>
      <div>
        <h3 style={contentStyle}>3</h3>
      </div>
      <div>
        <h3 style={contentStyle}>4</h3>
      </div>
    </Carousel>
    <br />
    <Carousel arrows dotPlacement="start" infinite={false}>
      <div>
        <h3 style={contentStyle}>1</h3>
      </div>
      <div>
        <h3 style={contentStyle}>2</h3>
      </div>
      <div>
        <h3 style={contentStyle}>3</h3>
      </div>
      <div>
        <h3 style={contentStyle}>4</h3>
      </div>
    </Carousel>
  </>
);

export default App;

점(dot)의 진행 표시 (Progress of dots)

점(dot)의 진행을 표시해요.

import React from 'react';
import { Carousel } from 'antd';

const contentStyle: React.CSSProperties = {
  margin: 0,
  height: '160px',
  color: '#fff',
  lineHeight: '160px',
  textAlign: 'center',
  background: '#364d79',
};

const App: React.FC = () => (
  <Carousel autoplay={{ dotDuration: true }} autoplaySpeed={5000}>
    <div>
      <h3 style={contentStyle}>1</h3>
    </div>
    <div>
      <h3 style={contentStyle}>2</h3>
    </div>
    <div>
      <h3 style={contentStyle}>3</h3>
    </div>
    <div>
      <h3 style={contentStyle}>4</h3>
    </div>
  </Carousel>
);

export default App;

API

공통 props 참고: Common props

속성 설명 타입 기본값 버전 전역 설정
arrows 전환 화살표 표시 여부 boolean false 5.17.0 ×
autoplay 자동 스크롤 여부, autoplay={{ dotDuration: true }}로 진행 막대 표시 가능 boolean | { dotDuration?: boolean } false dotDuration: 5.24.0 ×
autoplaySpeed 각 자동 스크롤 사이의 지연(밀리초) number 3000 ×
adaptiveHeight 슬라이드 높이 자동 조절 boolean false ×
dotPlacement 점의 위치, top bottom start end 중 하나 string bottom ×
dotPosition 점의 위치, top bottom left right start end 중 하나. dotPlacement를 사용하세요 string bottom ×
dots 갤러리 하단 점 표시 여부, dotsClass용 object boolean | { className?: string } true ×
draggable 데스크톱에서 드래그로 스크롤 가능 boolean false ×
fade 페이드 전환 사용 여부 boolean false ×
infinite 콘텐츠를 무한히 순환 boolean true ×
speed 애니메이션 속도(밀리초) number 500 ×
easing 전환 보간 함수 이름 string linear ×
effect 전환 효과 scrollx | fade scrollx ×
afterChange 현재 인덱스가 바뀐 후 호출되는 콜백 (current: number) => void - ×
beforeChange 현재 인덱스가 바뀌기 전에 호출되는 콜백 (current: number, next: number) => void - ×
waitForAnimate 전환 시 애니메이션을 기다릴지 여부 boolean false ×

더 많은 API는 react-slick 문서에서 찾을 수 있어요.

메서드 (Methods)

이름 설명
goTo(slideNumber, dontAnimate) 슬라이드 인덱스로 이동. dontAnimate=true면 애니메이션 없이 이동
next() 현재 슬라이드를 다음 슬라이드로 변경
prev() 현재 슬라이드를 이전 슬라이드로 변경

디자인 토큰 (Design Token)

토큰 이름 설명 타입 기본값
arrowOffset Carousel 가장자리로부터 화살표 오프셋 number 8
arrowSize 화살표 크기 number 16
dotActiveWidth 활성 표시자의 너비 string | number 24
dotGap 표시자 사이 간격 number 4
dotHeight 표시자의 높이 string | number 3
dotOffset Carousel 가장자리로부터 점 오프셋 number 12
dotWidth 표시자의 너비 string | number 16

전역 토큰 (Global Token)

토큰 이름 설명 타입 기본값
colorBgContainer 컨테이너 배경색. 예: 기본 버튼, 입력박스 등. colorBgElevated와 혼동하지 말 것. string
colorText W3C 표준을 준수하는 기본 텍스트 색. 가장 어두운 중성색이기도 함. string
fontFamily 시스템 기본 인터페이스 폰트와 화면 표시에 적합한 대체 폰트 라이브러리 세트 제공 string
fontSize 디자인 시스템에서 가장 널리 사용되는 폰트 크기. number
lineHeight 텍스트의 줄 높이. number
marginXXS 요소의 여백 제어, 가장 작은 크기. number
motionDurationSlow 모션 속도, 느린 속도. 대형 요소 애니메이션 상호작용에 사용. string

FAQ

커스텀 화살표를 어떻게 추가하나요? {#faq-add-custom-arrows}

#12479 참고.

더 알아보기 (Learn more)