πŸ’™ 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