๐ค ์์งlog ๐ค
[REACT] Swiper ์ฌ์ฉํ์ฌ ํฐ์น์ฌ๋ผ์ด๋ ๊ตฌํํ๊ธฐ ๋ณธ๋ฌธ
์ข <-> ์ฐ ๋ก ์นดํ ๊ณ ๋ฆฌ ์ด๋ ๋ฐ ํฐ์น์ฌ๋ผ์ด๋๋ฅผ ๊ตฌํํด์ผ ํ๋ ์ํฉ์ด์๋๋ฐ
๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ฌ์ฉ์์ด ์ฒ์๋ถํฐ ๊ตฌํํ๊ณ ์ถ์์ง๋ง ์์ง ๋ด์ค๋ ฅ์ผ๋ก ๋ฌด๋ฆฌ,, ์ใธใ ;,,
์ฌ๋ฌ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ๋ง์์ง๋ง ๋๋ Swiper ์ผ๋ก ๊ตฌํํด๋ณด๊ธฐ๋ก ํ์๋ค !
๐ Swiper ์ค์น
npm install swiper
๐ Swiper ์์ ์ฝ๋
import { Swiper, SwiperSlide } from 'swiper/react';
import { Pagination, Mousewheel, Keyboard } from 'swiper';
import 'swiper/css';
import 'swiper/css/pagination';
function () {
return (
<Swiper
direction={'vertical'} // ์ฌ๋ผ์ด๋์ ๋ฐฉํฅ์ ์ค์ ํฉ๋๋ค. ( "vertical"/"horizontal" )
pagination={{ clickable: true }} // dots๋ฅผ ํด๋ฆญํ์ ๋, ํด๋ฆญํ ์ฌ๋ผ์ด๋๋ก ์ด๋ํ๊ฒ ๋ฉ๋๋ค.
mousewheel // ๋ง์ฐ์ค ํ ๋์์ ํ์ฉํฉ๋๋ค.
keyboard // ํค๋ณด๋ ๋ฐฉํฅํค์ ์ํ ๋์์ ํ์ฉํฉ๋๋ค.
modules={[Pagination, Mousewheel, Keyboard]} // ํ์ด์ง๋ค์ด์
, ๋ง์ฐ์คํ , ํค๋ณด๋ ๋ฑ์ ์ฌ์ฉํ๋ ค๋ฉด ๋ชจ๋์ importํด์ค์ผ ํฉ๋๋ค.
allowTouchMove // ํฐ์น ๋์์ ํ์ฉํฉ๋๋ค.
className="main_slider"
threshold={20} // ํฐ์น ๊ฐ๋๋ฅผ ์กฐ์ ํฉ๋๋ค. ์ซ์๊ฐ ํด์๋ก ํฐ์น์ ๋ฐ์ํ์ง ์์ต๋๋ค.
speed={1000} // ์ฌ๋ผ์ด๋๊ฐ ๋์ด๊ฐ๋ ์๋๋ฅผ ์กฐ์ ํฉ๋๋ค. ๋จ์๋ ms์
๋๋ค.
onActiveIndexChange={(swiper) => {
console.log(swiper.activeIndex);
}}
>
<SwiperSlide>Slide 1</SwiperSlide>
<SwiperSlide>Slide 2</SwiperSlide>
<SwiperSlide>Slide 3</SwiperSlide>
<SwiperSlide>Slide 4</SwiperSlide>
<SwiperSlide>Slide 5</SwiperSlide>
</Swiper>
);
}
export default;
์ ์ฝ๋์์๋ ๊ธฐ๋ณธ์ ์ผ๋ก ํ์ํ ์ต์ ๋ค๋ง ์ค์ ํ์ต๋๋ค. ์ต์ ์ด ๊ต์ฅํ ๋ง์ด ์๊ธฐ ๋๋ฌธ์ Swiper ๊ณต์๋ฌธ์์์ ์ต์ ๋ค์ ์ฐพ์๋ณด๋ ๊ฒ์ ๊ถ์ฅ๋๋ฆฝ๋๋ค.
๐ ๊ตฌํ ์ฝ๋
import { Pagination, Keyboard } from "swiper";
import { Swiper, SwiperSlide } from "swiper/react";
import "swiper/css";
import styled from "styled-components";
import { MENU_LIST, PAGE_LIST } from "datas/main";
const Main = () => {
return (
<Container>
<CustomSwiper
pagination={{
clickable: true,
el: ".swiper-pagination",
renderBullet: function (index, className) {
return (
'<span class="' + className + '">' + PAGE_LIST[index] + "</span>"
);
},
}}
modules={[Pagination, Keyboard]}
keyboard
speed={1000}
loop={true}
>
{MENU_LIST.map(({ id, menu }) => (
<CustomSwiperSlide key={id}>{menu}</CustomSwiperSlide>
))}
<div className="swiper-pagination" />
</CustomSwiper>
</Container>
);
};
const Container = styled.div`
width: 100%;
display: flex;
justify-content: center;
margin: 0 auto;
max-height: 900px;
`;
const CustomSwiper = styled(Swiper)`
.swiper-pagination {
position: absolute;
top: 0;
left: 0;
display: flex;
align-items: center;
justify-content: space-evenly;
gap: 20px;
background-color: lightgreen;
width: 425px;
height: 40px;
padding: 0 20px;
box-sizing: border-box;
.swiper-pagination-bullet {
font-size: 15px;
font-weight: 800;
cursor: pointer;
color: #000;
}
.swiper-pagination-bullet-active {
color: #fff;
}
}
`;
const CustomSwiperSlide = styled(SwiperSlide)`
margin-top: 40px;
`;
export default Main;
๐ฅ ์์ฑ ..! ๐ฅ
+ ๋ชจ๋ฐ์ผ๋ก๋ ํฐ์น ์ฌ๋ผ์ด๋๊ฐ ๊ฐ๋ฅํ๋ค! (๋ฟ๋ฏโค๏ธ)
728x90
'๐ React' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[REACT] CSS ๋ฐ์ํ ๋ ์ด์์ ๋๋น๋ฅผ ๋๋๋ ๊ธฐ์ค ์ ํ๊ธฐ (0) | 2023.02.10 |
---|---|
[REACT] Carousel๋ก ๋ฐฐ๋ ์ฌ๋ผ์ด๋ ๊ตฌํํ๊ธฐ (0) | 2023.02.06 |
[REACT] ์ด๋ฒคํธ ํธ๋ค๋ฌ ( Event handler ) (0) | 2022.10.07 |
[REACT] ๋ฐ๋ณต ( Iteration ) (0) | 2022.10.07 |
[REACT] HOOK (0) | 2022.10.07 |