Skip to content

Commit

Permalink
feat: 캐러셀 슬라이드 효과 적용
Browse files Browse the repository at this point in the history
이론상 맞는데... 모바일에서만 확인 가능하다네요... 다른 방법 일단
찾아보겠습니다!
  • Loading branch information
sinji2102 committed Aug 13, 2024
1 parent fbffae6 commit 1493d26
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/pages/main/components/carousel/Carousel.styled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export const CarouselWarpper = styled.div`
`;

export const CarouselLayout = styled.div`
/* position: relative; */
z-index: 1;
display: flex;
align-items: center;
justify-content: center;
Expand Down
52 changes: 50 additions & 2 deletions src/pages/main/components/carousel/Carousel.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useRef, useState } from "react";
import { TouchEventHandler, useEffect, useRef, useState } from "react";
import { useNavigate } from "react-router-dom";
import * as S from "./Carousel.styled";

Expand All @@ -12,6 +12,9 @@ interface PromotionComponentProps {
promotionList: PromotionProps[];
}

let touchStartX: number;
let touchEndX: number;

const Carousel = ({ promotionList }: PromotionComponentProps) => {
const navigate = useNavigate();

Expand Down Expand Up @@ -79,9 +82,54 @@ const Carousel = ({ promotionList }: PromotionComponentProps) => {
};
}, [currIndex]);

const handleSwipe = (direction: number) => {
const newIndex = currIndex + direction;

if (newIndex === carouselList.length + 1) {
moveToNthSlide(1);
} else if (newIndex === 0) {
moveToNthSlide(carouselList.length);
}

setCurrIndex((prev) => prev + direction);
if (carouselRef.current !== null) {
carouselRef.current.style.transition = "all 0.5s ease-in-out";
}
};

const handleTouchStart: TouchEventHandler<HTMLDivElement> = (e) => {
touchStartX = e.touches[0].clientX;
console.log(e.touches[0].clientX, "start!");
};

const handleTouchMove: TouchEventHandler<HTMLDivElement> = (e) => {
const currTouchX = e.nativeEvent.changedTouches[0].clientX;

if (carouselRef.current !== null) {
carouselRef.current.style.transform = `translateX(calc(-${currIndex}00% - ${
(touchStartX - currTouchX) * 2 || 0
}px))`;
}
};

const handleTouchEnd: TouchEventHandler<HTMLDivElement> = (e) => {
touchEndX = e.changedTouches[0].clientX;
console.log(e.changedTouches[0].clientX, "end!");

if (touchStartX >= touchEndX) {
handleSwipe(1);
} else {
handleSwipe(-1);
}
};

return (
<S.CarouselWarpper>
<S.CarouselLayout>
<S.CarouselLayout
onTouchStart={handleTouchStart}
onTouchMove={handleTouchMove}
onTouchEnd={handleTouchEnd}
>
<S.CarouselContainer ref={carouselRef}>
{currList?.map((image, idx) => {
const key = `${image}-${idx}`;
Expand Down

0 comments on commit 1493d26

Please sign in to comment.