Library React Swiper 응용하기 (module, props...)
Library React Swiper 슬라이드 가능한 컴포넌트 만들기
React Library 중 하나인 React Swiper를 사용해보고자 한다. 업무 중에 생각보다 많이 쓰이기에 한번 정리하고 넘어가야 할것 같다. 오늘은 기본적인 사용법과 문법에 대해 알아보자. 1. Package Install npm
3pdev.tistory.com
기본편에서 말했듯 React Swiper는 여러가지 모듈, props, style등을 활용해 자유도를 높였다.
먼저 모듈부터 살펴보자.
Navigation 모듈은 슬라이더에 이전/다음 버튼을 추가한다. 클릭하면 슬라이더가 넘어간다.
import SwiperCore, { Navigation } from 'swiper';
import 'swiper/css/navigation';
SwiperCore.use([Navigation]);
<Swiper navigation={true}>
...
</Swiper>
import SwiperCore, { Pagination } from 'swiper';
import 'swiper/css/pagination';
SwiperCore.use([Pagination]);
<Swiper pagination={true}>
...
</Swiper>
import SwiperCore, { Autoplay } from 'swiper';
SwiperCore.use([Autoplay]);
<Swiper autoplay={{ delay: 3000,disableOnInteraction: false, }}>
...
</Swiper>
import SwiperCore, { EffectCube } from 'swiper';
SwiperCore.use([EffectCube]);
<Swiper effect={'cube'}>
...
</Swiper>
import SwiperCore, { Virtual } from 'swiper';
SwiperCore.use([Virtual]);
<Swiper virtual>
...
</Swiper>
import SwiperCore, { Keyboard } from 'swiper';
SwiperCore.use([Keyboard]);
<Swiper keyboard={{ enabled: true,onlyInViewport: true, }}>
...
</Swiper>
import SwiperCore, { Mousewheel } from 'swiper';
SwiperCore.use([Mousewheel]);
<Swiper mousewheel={{forceToAxis: true, releaseOnEdges: true, }}>
...
</Swiper>
다음은 swiper에서 제공하는 주요 props를 정리했다.
slidesPerView: 한 페이지에 보여줄 슬라이드 개수를 설정하여, 한 번에 여러 슬라이드를 보여줄 수 있게 해준다.
<Swiper slidesPerView={3}>
<SwiperSlide>Slide 1</SwiperSlide>
<SwiperSlide>Slide 2</SwiperSlide>
<SwiperSlide>Slide 3</SwiperSlide>
</Swiper>
spaceBetween: 슬라이드 간 간격을 설정하여, 각 슬라이드 간의 간격을 조절할 수 있게 해준다.
<Swiper spaceBetween={20}>
<SwiperSlide>Slide 1</SwiperSlide>
<SwiperSlide>Slide 2</SwiperSlide>
<SwiperSlide>Slide 3</SwiperSlide>
</Swiper>
centeredSlides: 슬라이드를 중앙 정렬할지 여부를 설정하여, 슬라이드가 중앙에 위치하도록 할 수 있게 해준다.
<Swiper centeredSlides>
<SwiperSlide>Slide 1</SwiperSlide>
<SwiperSlide>Slide 2</SwiperSlide>
<SwiperSlide>Slide 3</SwiperSlide>
</Swiper>
initialSlide: 슬라이드 시작 인덱스를 설정하여, 슬라이더를 특정 슬라이드에서 시작할 수 있게 해준다.
<Swiper initialSlide={2}>
<SwiperSlide>Slide 1</SwiperSlide>
<SwiperSlide>Slide 2</SwiperSlide>
<SwiperSlide>Slide 3</SwiperSlide>
</Swiper>
loop: 슬라이드 루핑 여부를 설정하여, 슬라이드를 처음으로 돌아가게 하거나 무한대로 반복할 수 있게 해준다.
<Swiper loop>
<SwiperSlide>Slide 1</SwiperSlide>
<SwiperSlide>Slide 2</SwiperSlide>
<SwiperSlide>Slide 3</SwiperSlide>
</Swiper>
autoplay: 자동 슬라이드 전환 여부와 시간 간격을 설정하여, 슬라이드를 자동으로 이동시키게 해준다.
<Swiper autoplay={{ delay: 3000 }}>
<SwiperSlide>Slide 1</SwiperSlide>
<SwiperSlide>Slide 2</SwiperSlide>
<SwiperSlide>Slide 3</SwiperSlide>
</Swiper>
navigation: 이전/다음 버튼 사용 여부와 설정하여, 슬라이드 이동을 버튼으로 제어할 수 있게 해준다.
<Swiper navigation>
<SwiperSlide>Slide 1</SwiperSlide>
<SwiperSlide>Slide 2</SwiperSlide>
<SwiperSlide>Slide 3</SwiperSlide>
</Swiper>
pagination: 페이지 매김 사용 여부와 설정하여, 슬라이드의 페이지 매김을 보여줄 수 있게 해준다.
<Swiper pagination={{ clickable: true }}>
<SwiperSlide>Slide 1</SwiperSlide>
<SwiperSlide>Slide 2</SwiperSlide>
<SwiperSlide>Slide 3</SwiperSlide>
</Swiper>
effect: 슬라이드 전환 효과를 설정하여, 다양한 전환 효과를 사용할 수 있게 해준다.
<Swiper effect="fade">
<SwiperSlide>Slide 1</SwiperSlide>
<SwiperSlide>Slide 2</SwiperSlide>
<SwiperSlide>Slide 3</SwiperSlide>
</Swiper>
virtual: 매우 큰 슬라이드 데이터를 처리할 때 사용하여, 가상 슬라이드를 생성할 수 있게 해준다.
<Swiper virtual>
<SwiperSlide>Slide 1</SwiperSlide>
<SwiperSlide>Slide 2</SwiperSlide>
<SwiperSlide>Slide 3</SwiperSlide>
</Swiper>
간략하게 props로 사용하는 방법만 적어보았다.
좋아보인다고 막 사용하는 것이 아니라 기획한 페이지와 맞는가, 이슈는 발생하지 않는가 등을 고려하여
작업중인 프로젝트에 알맞는 기능들로 응용하면 좋을 것 같다.
글을 작성하기 위해, swiper를 사용하기 위해 참고했던 문서