diff --git a/public/img/banner1.png b/public/img/banner1.png new file mode 100644 index 0000000..814685b Binary files /dev/null and b/public/img/banner1.png differ diff --git a/public/img/banner2.png b/public/img/banner2.png new file mode 100644 index 0000000..aed23b8 Binary files /dev/null and b/public/img/banner2.png differ diff --git a/public/img/banner3.png b/public/img/banner3.png new file mode 100644 index 0000000..55e5239 Binary files /dev/null and b/public/img/banner3.png differ diff --git a/public/img/banner4.png b/public/img/banner4.png new file mode 100644 index 0000000..c06d5e6 Binary files /dev/null and b/public/img/banner4.png differ diff --git a/public/img/banner_temp.png b/public/img/banner_temp.png deleted file mode 100644 index 6a346cc..0000000 Binary files a/public/img/banner_temp.png and /dev/null differ diff --git a/src/pages/main/Home.tsx b/src/pages/main/Home.tsx index 4ef1aa9..7e334dc 100644 --- a/src/pages/main/Home.tsx +++ b/src/pages/main/Home.tsx @@ -7,7 +7,7 @@ import categories from "../../utils/categories"; import { getCookie } from "../../utils/cookie"; import { useQuery, useQueryClient } from "@tanstack/react-query"; import { teamApi } from "../../apis/domains/teamApi"; -import { useEffect } from "react"; +import { useEffect, useRef, useState } from "react"; const Home = () => { const navigate = useNavigate(); @@ -44,6 +44,80 @@ const Home = () => { } }, [isError]); + const [currentIndex, setCurrentIndex] = useState(0); + const [isDragging, setIsDragging] = useState(false); + const [startPosition, setStartPosition] = useState(0); + const [currentTranslate, setCurrentTranslate] = useState(0); + const [prevTranslate, setPrevTranslate] = useState(0); + const sliderRef = useRef(null); + const slideItems = [ + "/img/banner1.png", + "/img/banner2.png", + "/img/banner3.png", + "/img/banner4.png", + ]; + + useEffect(() => { + const handleResize = () => { + setCurrentTranslate(0); + setPrevTranslate(0); + setCurrentIndex(0); + }; + + window.addEventListener("resize", handleResize); + return () => window.removeEventListener("resize", handleResize); + }, []); + + const handleMouseDown = (event: React.MouseEvent) => { + setIsDragging(true); + setStartPosition(event.clientX); + }; + + const handleMouseMove = (event: React.MouseEvent) => { + if (isDragging) { + const currentPosition = event.clientX; + const diff = currentPosition - startPosition; + setCurrentTranslate(prevTranslate + diff); + } + }; + + const handleMouseUp = () => { + setIsDragging(false); + const movedBy = currentTranslate - prevTranslate; + + if (movedBy < -100 && currentIndex < slideItems.length - 1) { + setCurrentIndex(currentIndex + 1); + } + + if (movedBy > 100 && currentIndex > 0) { + setCurrentIndex(currentIndex - 1); + } + + setPrevTranslate(currentIndex * -sliderRef.current!.offsetWidth); + setCurrentTranslate(currentIndex * -sliderRef.current!.offsetWidth); + }; + + const handleMouseLeave = () => { + if (isDragging) { + handleMouseUp(); + } + }; + + const handleSlide = (index: number) => { + setCurrentIndex(index); + const newTranslate = index * -sliderRef.current!.offsetWidth; + setPrevTranslate(newTranslate); + setCurrentTranslate(newTranslate); + }; + + useEffect(() => { + const slider = sliderRef.current; + if (slider) { + slider.style.transform = `translateX(${currentTranslate}px)`; + slider.style.transition = "transform 0.3s ease-out"; + } + }, [currentTranslate]); + return (
@@ -69,7 +143,36 @@ const Home = () => {
{/* 캐러셀 구역 */} - banner +
+
+ {slideItems.map((item, index) => ( +
+ ))} +
+ {/* 슬라이드 인디케이터 */} +
+ {slideItems.map((_, index) => ( +
handleSlide(index)} + >
+ ))} +
+