π React
[REACT] Swiper μ¬μ©νμ¬ ν°μΉμ¬λΌμ΄λ ꡬννκΈ°
Eun_zii
2023. 2. 6. 12:34
μ’ <-> μ° λ‘ μΉ΄ν κ³ λ¦¬ μ΄λ λ° ν°μΉμ¬λΌμ΄λλ₯Ό ꡬνν΄μΌ νλ μν©μ΄μλλ°
λΌμ΄λΈλ¬λ¦¬ μ¬μ©μμ΄ μ²μλΆν° ꡬννκ³ μΆμμ§λ§ μμ§ λ΄μ€λ ₯μΌλ‘ 무리,, μγΈγ ;,,
μ¬λ¬ λΌμ΄λΈλ¬λ¦¬κ° λ§μμ§λ§ λλ 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