๐Ÿค ์€์ง€log ๐Ÿค

[REACT] Swiper ์‚ฌ์šฉํ•˜์—ฌ ํ„ฐ์น˜์Šฌ๋ผ์ด๋“œ ๊ตฌํ˜„ํ•˜๊ธฐ ๋ณธ๋ฌธ

๐Ÿ’™ 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;

 

๐Ÿฅ ์™„์„ฑ ..! ๐Ÿฅ

 

web์—์„œ๋Š” ํ„ฐ์น˜๋กœ ์ด๋™์ด ํž˜๋“ค๊ธฐ์— keyboard ๋ฐฉํ–ฅํ‚ค๋กœ ์ด๋™ํ• ์ˆ˜ ์žˆ๊ฒŒ ํ•˜์˜€๋‹ค.

 

+ ๋ชจ๋ฐ”์ผ๋กœ๋Š” ํ„ฐ์น˜ ์Šฌ๋ผ์ด๋“œ๊ฐ€ ๊ฐ€๋Šฅํ–ˆ๋‹ค! (๋ฟŒ๋“ฏโค๏ธ)

 

728x90