Next.js - 이미지 캐러셀(미니)
- 하나의 이미지 박스에서 이미지들이 슬라이더 되는 상황
"use client";
import { useEffect, useState } from "react";
import Image from "next/image";
import burgerImg from "@/assets/burger.jpg";
import curryImg from "@/assets/curry.jpg";
import dumplingsImg from "@/assets/dumplings.jpg";
import macncheeseImg from "@/assets/macncheese.jpg";
import pizzaImg from "@/assets/pizza.jpg";
import schnitzelImg from "@/assets/schnitzel.jpg";
import tomatoSaladImg from "@/assets/tomato-salad.jpg";
import styles from "./imageSlideShow.module.css";
const images = [
{ image: burgerImg, alt: "A delicious, juicy burger" },
{ image: curryImg, alt: "A delicious, spicy curry" },
{ image: dumplingsImg, alt: "Steamed dumplings" },
{ image: macncheeseImg, alt: "Mac and cheese" },
{ image: pizzaImg, alt: "A delicious pizza" },
{ image: schnitzelImg, alt: "A delicious schnitzel" },
{ image: tomatoSaladImg, alt: "A delicious tomato salad" },
];
export default function ImageSlideshow() {
const [currentImageIndex, setCurrentImageIndex] = useState(0);
useEffect(() => {
const interval = setInterval(() => {
setCurrentImageIndex((prevIndex) =>
prevIndex < images.length - 1 ? prevIndex + 1 : 0
);
}, 5000);
return () => clearInterval(interval);
}, []);
return (
<div className={styles.slideshow}>
{images.map((image, index) => (
<Image
key={index}
src={image.image}
className={index === currentImageIndex ? styles.active : ""}
alt={image.alt}
/>
))}
</div>
);
}