반응형
html에서 swiper 사용하는 것과 조금 다르기 때문에 React에서도 swiper를 사용해 보려고 한다
Swiper React Components
Swiper is the most modern free mobile touch slider with hardware accelerated transitions and amazing native behavior.
swiperjs.com
① swiper 설치하기
npm install --save swiper
② 프로젝트/src/components/MainPage/MainPage.js
import { Swiper, SwiperSlide } from 'swiper/react';
import SwiperCore, { Navigation, Pagination } from "swiper";
import "swiper/swiper.scss";
import "swiper/components/navigation/navigation.scss";
import "swiper/components/pagination/pagination.scss";
SwiperCore.use([Navigation, Pagination])
function MainPage() {
return(
<div>
<Swiper
className="banner"
spaceBetween={50}
slidesPerView={1}
navigation
pagination={{ clickable: true }}
>
<SwiperSlide>Slide 1</SwiperSlide>
<SwiperSlide>Slide 2</SwiperSlide>
<SwiperSlide>Slide 3</SwiperSlide>
<SwiperSlide>Slide 4</SwiperSlide>
</Swiper>
</div>
)
}
export default MainPage;
scrollbar도 사용 가능하지만 난 navigation과 pagination만 사용했다
scss 파일을 import할 때 에러가 발생한다면 sass 설치를 해야 한다
npm install --save sass
⑴ navigation
< > 화살표로 슬라이드를 교체할 수 있다
⑵ pagination
하단 중앙에 있는 원형 버튼을 클릭하면 해당 슬라이드로 넘어간다
navigation, pagination 외에도 다른 모듈들이 많다
이 중에서 자동으로 슬라이드를 넘기는 Autoplay를 추가로 사용해 보자
③ 프로젝트/src/components/MainPage/MainPage.js 수정
import { Swiper, SwiperSlide } from 'swiper/react';
import SwiperCore, { Navigation, Pagination, Autoplay } from "swiper"; // 추가
import "swiper/swiper.scss";
import "swiper/components/navigation/navigation.scss";
import "swiper/components/pagination/pagination.scss";
SwiperCore.use([Navigation, Pagination, Autoplay]) // 추가
function MainPage() {
return(
<div>
<Swiper
className="banner"
spaceBetween={50}
slidesPerView={1}
navigation
pagination={{ clickable: true }}
autoplay={{ delay: 1000 }} // 추가
>
<SwiperSlide>Slide 1</SwiperSlide>
<SwiperSlide>Slide 2</SwiperSlide>
<SwiperSlide>Slide 3</SwiperSlide>
<SwiperSlide>Slide 4</SwiperSlide>
</Swiper>
</div>
)
}
export default MainPage;
delay를 1000으로 줬기 때문에 1초에 하나씩 자동으로 넘어가게 된다
반응형
'React&React-Native > React' 카테고리의 다른 글
[#. React] Web/Mobile 웹/모바일 토글 메뉴바 toggle menubar 추가하기 (0) | 2021.04.15 |
---|---|
[#. React] react-device-detect 설치 후 웹/모바일 감지해서 반응형 사이트 만들기 (0) | 2021.04.12 |
[#. React] Kakao 카카오 지도 API를 이용해서 키워드로 장소 검색하기, 검색한 장소 마커 띄우기, 검색 결과 리스트 가져오기 2 (0) | 2021.04.07 |
[#. React] Kakao 카카오 지도 API를 이용해서 키워드로 장소 검색하기, 검색한 장소 마커 띄우기 (0) | 2021.04.06 |
[#. React] React에서 checkbox select all 체크박스 전체 선택, 전체 선택 해제 하기 (0) | 2021.04.01 |