[React Swiper] play, stop 버튼 만들기

2023. 4. 13. 21:52·Frontend/React

* 참고 문서

https://swiperjs.com/swiper-api#autoplay

 

Swiper API

Swiper is the most modern free mobile touch slider with hardware accelerated transitions and amazing native behavior.

swiperjs.com

 

<Swiper
  modules={[Pagination, A11y, Controller, Navigation, Autoplay]}
  pagination={{ type: 'fraction' }}
                loop={true}
  navigation={true}
                initialSlide={1}
  autoplay={{
    disableOnInteraction: true,
  }}
  className=""
>
	<SwiperSlide></SwiperSlide>
</Swiper>

Autoplay를 모듈에 작성해준다.

 

- disableOnInteraction -> 내가 만약 임의로 페이지를 넘겼을 때 (기본값이 true)

true : 자동 재생이 멈춤 (재생버튼이 있으면 재생버튼을 눌러줘야지 다시 autoplay가 된다)

false : 내가 임의로 넘긴 페이지부터 자동재생이 계속된다.

 


자동 재생 버튼 컴포넌트 만들기

 

아래 stackoverflow의 첫번째 댓글을 참고하면 좋을 것 같다.

https://stackoverflow.com/questions/71617336/how-to-use-useswiper-outside-a-swiper-instance

 

How to use useSwiper outside a Swiper instance?

I'm using Swiper for React to show some slides. I'm stuck at using external buttons to navigate between slides (previous and next). Swiper has a useSwiper hook that can provide programmatic access ...

stackoverflow.com

 

자동재생 버튼을 만들 때는 따로 컴포넌트로 빼서 작업하는 편이 젤 best이다.

useSwiper must be used by components inside a Swiper element. --> 무조건 스와이퍼 안에 선언해줘야한다.

 

SwiperButtonPlay 컴포넌트를 만들었다.

import Link from 'next/link';
import React, { Dispatch, SetStateAction, useState } from 'react';
import { useSwiper } from 'swiper/react';

export type playProps = {
	playToggle: boolean;
	setPlayToggle : Dispatch<SetStateAction<boolean>>;
}

function SwiperButtonPlay ( props: playProps) {
	const swiper = useSwiper();
	const handlePlay = () => {
		if (props.playToggle) {
			swiper.autoplay.stop();
			props.setPlayToggle(false);
		}else{
			swiper.autoplay.start();
			props.setPlayToggle(true);
		}
	}
	
	return <button className={props.playToggle ? 'btn-play--off' : 'btn-play'} onClick={handlePlay} title={props.playToggle ? '정지버튼' : '재생버튼'}></button>;
}

export default SwiperButtonPlay ;

 

 

위의 컴포넌트를 내가 쓰려는 Swiper 안에 작성해주면된다.

const [playToggle, setPlayToggle] = useState(true);



<Swiper
  modules={[Pagination, A11y, Controller, Navigation, Autoplay]}
  pagination={{ type: 'fraction' }}
  loop={true}
  navigation={true}
  autoplay={{
    disableOnInteraction: false,
  }}
  className="swiper-wrapper"
>
  <SwiperSlide></SwiperSlide>
  
  <div className="playbtn__wrapper">
    <SwiperButtonPlay playToggle={playToggle} setPlayToggle={setPlayToggle} />
  </div>
</Swiper>

 

반응형

'Frontend > React' 카테고리의 다른 글

[React Swiper] loop={true} 마지막 페이지 먼저보이는 에러  (0) 2023.04.13
[React Swiper] import css 안될 때  (1) 2023.04.13
[React Swiper] 리액트에서 스와이퍼 사용하기  (0) 2023.04.13
[React] 리액트 순위 매기기 만들기 (ranking)  (0) 2023.02.26
리액트 연습하기  (1) 2022.11.16
'Frontend/React' 카테고리의 다른 글
  • [React Swiper] loop={true} 마지막 페이지 먼저보이는 에러
  • [React Swiper] import css 안될 때
  • [React Swiper] 리액트에서 스와이퍼 사용하기
  • [React] 리액트 순위 매기기 만들기 (ranking)
전예방
전예방
  • 전예방
    예방이의 개발일기
    전예방
  • 전체
    오늘
    어제
    • All (125)
      • Info & Tip (2)
      • 유용한 사이트들 (5)
      • Mark Up & StyleSheet (23)
        • HTML (6)
        • CSS & SCSS (10)
        • 반응형 (6)
      • Frontend (66)
        • 전체 (10)
        • JavaScript & Jquery (18)
        • TypeScript (0)
        • React (26)
        • Next.js (3)
        • 성능최적화 (2)
        • 웹접근성 (2)
      • Backend (13)
        • Python (1)
        • JAVA (2)
        • node.js (0)
        • PHP (7)
        • 패키지매니저 (3)
      • Markdown (0)
      • SCM (1)
        • Git&Github (1)
        • SVN (0)
      • IDE (4)
        • VSCode (2)
        • IntelliJ (1)
        • Ecplise (1)
      • 취미생활 (3)
      • 정보처리기사 (2)
      • 코딩자율학습단 (5)
        • 12기 (파이썬) (5)
  • 블로그 메뉴

    • 홈
    • 태그
  • 링크

    • velog
    • github
  • 공지사항

  • 인기 글

  • 태그

    frontend
    swiper.js
    React Swiper
    NPM
    Python
    CSS
    yarn berry
    php
    react
    Chart.js
    파이썬
    리액트
    회원가입
    코딩자율학습
    Admin
    관리자 페이지
    TypeScript
    나도코딩
    코딩자율학습단
    반응형
  • 최근 댓글

  • 최근 글

  • 반응형
  • hELLO· Designed By정상우.v4.10.3
전예방
[React Swiper] play, stop 버튼 만들기
상단으로

티스토리툴바