2025-11-18 15:11:13 +08:00
|
|
|
import * as React from "react";
|
2025-11-19 11:45:10 +08:00
|
|
|
import Autoplay from "embla-carousel-autoplay";
|
2025-11-18 13:30:19 +08:00
|
|
|
|
2025-11-18 15:29:20 +08:00
|
|
|
import { Card, CardContent } from "@/ui/card";
|
2025-11-18 13:30:19 +08:00
|
|
|
import {
|
2025-11-19 11:45:10 +08:00
|
|
|
Carousel,
|
|
|
|
|
CarouselContent,
|
|
|
|
|
CarouselItem,
|
2025-11-18 13:30:19 +08:00
|
|
|
CarouselNext,
|
|
|
|
|
CarouselPrevious,
|
2025-11-18 15:11:13 +08:00
|
|
|
type CarouselApi,
|
2025-11-18 15:29:20 +08:00
|
|
|
} from "@/ui/carousel";
|
2025-11-18 19:47:03 +08:00
|
|
|
|
2025-11-19 11:45:10 +08:00
|
|
|
const imageUrls = [
|
|
|
|
|
"/images/carousel-1.jpg",
|
|
|
|
|
"/images/carousel-2.jpg",
|
|
|
|
|
"/images/carousel-3.jpg",
|
|
|
|
|
"/images/carousel-4.jpg",
|
|
|
|
|
"/images/carousel-5.jpg",
|
|
|
|
|
"/images/carousel-6.jpg",
|
|
|
|
|
];
|
2025-11-18 13:30:19 +08:00
|
|
|
|
2025-11-20 08:00:40 +08:00
|
|
|
export interface CarouselDemoProps {
|
|
|
|
|
paginationPosition?: 'left' | 'right'; // 默认右下角
|
|
|
|
|
paginationStyle?: 'dot' | 'bar'; // 默认圆形
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function CarouselDemo({
|
|
|
|
|
paginationPosition = 'right',
|
|
|
|
|
paginationStyle = 'dot',
|
|
|
|
|
}: CarouselDemoProps) {
|
2025-11-18 15:11:13 +08:00
|
|
|
const [api, setApi] = React.useState<CarouselApi>();
|
|
|
|
|
const [current, setCurrent] = React.useState(0);
|
|
|
|
|
const [count, setCount] = React.useState(0);
|
2025-11-19 16:30:50 +08:00
|
|
|
const totalSlides = imageUrls.length;
|
|
|
|
|
|
2025-11-18 15:11:13 +08:00
|
|
|
React.useEffect(() => {
|
|
|
|
|
if (!api) return;
|
|
|
|
|
|
|
|
|
|
setCount(api.scrollSnapList().length);
|
2025-11-19 11:45:10 +08:00
|
|
|
setCurrent(api.selectedScrollSnap());
|
|
|
|
|
|
2025-11-18 15:11:13 +08:00
|
|
|
api.on("select", () => {
|
|
|
|
|
setCurrent(api.selectedScrollSnap());
|
|
|
|
|
});
|
2025-11-19 11:45:10 +08:00
|
|
|
}, [api]);
|
2025-11-18 15:11:13 +08:00
|
|
|
|
2025-11-18 13:30:19 +08:00
|
|
|
return (
|
2025-11-19 11:45:10 +08:00
|
|
|
<div className="relative w-full h-full">
|
2025-11-18 15:11:13 +08:00
|
|
|
<Carousel
|
2025-11-19 11:45:10 +08:00
|
|
|
opts={{ loop: true }}
|
2025-11-18 15:11:13 +08:00
|
|
|
plugins={[
|
|
|
|
|
Autoplay({
|
|
|
|
|
delay: 3000,
|
2025-11-19 11:45:10 +08:00
|
|
|
stopOnInteraction: false,
|
2025-11-18 15:11:13 +08:00
|
|
|
}),
|
|
|
|
|
]}
|
|
|
|
|
setApi={setApi}
|
2025-11-19 11:45:10 +08:00
|
|
|
className="w-full h-full"
|
2025-11-18 15:11:13 +08:00
|
|
|
>
|
2025-11-19 15:36:23 +08:00
|
|
|
<CarouselContent className="h-full w-full -ml-0">
|
2025-11-20 08:00:40 +08:00
|
|
|
{imageUrls.map((src, index) => (
|
2025-11-19 15:36:23 +08:00
|
|
|
<CarouselItem key={index} className="w-full h-full pl-0">
|
|
|
|
|
<div className="p-0 w-full h-full">
|
2025-11-20 08:00:40 +08:00
|
|
|
<CardContent className="flex items-center justify-center p-0 w-full h-full m-0">
|
|
|
|
|
<img
|
|
|
|
|
src={src}
|
|
|
|
|
alt={`Slide ${index + 1}`}
|
|
|
|
|
className="w-full h-full object-cover"
|
|
|
|
|
loading="lazy"
|
|
|
|
|
/>
|
2025-11-19 16:30:50 +08:00
|
|
|
</CardContent>
|
2025-11-18 15:11:13 +08:00
|
|
|
</div>
|
|
|
|
|
</CarouselItem>
|
|
|
|
|
))}
|
|
|
|
|
</CarouselContent>
|
|
|
|
|
<CarouselPrevious className="absolute left-2 top-1/2 -translate-y-1/2 z-10" />
|
|
|
|
|
<CarouselNext className="absolute right-2 top-1/2 -translate-y-1/2 z-10" />
|
|
|
|
|
</Carousel>
|
|
|
|
|
|
2025-11-20 08:00:40 +08:00
|
|
|
{/* 分页指示器 */}
|
|
|
|
|
<div
|
|
|
|
|
className={`absolute bottom-4 ${
|
|
|
|
|
paginationPosition === 'left' ? 'left-4' : 'right-4'
|
|
|
|
|
} flex gap-2 z-10`}
|
|
|
|
|
>
|
2025-11-18 15:11:13 +08:00
|
|
|
{Array.from({ length: count }).map((_, index) => (
|
|
|
|
|
<button
|
|
|
|
|
key={index}
|
|
|
|
|
onClick={() => api?.scrollTo(index)}
|
2025-11-19 16:30:50 +08:00
|
|
|
className={`transition-colors ${
|
2025-11-20 08:00:40 +08:00
|
|
|
paginationStyle === 'dot'
|
|
|
|
|
? 'h-2 w-2 rounded-full'
|
|
|
|
|
: 'h-2 w-8 rounded'
|
|
|
|
|
} ${index === current ? 'bg-white' : 'bg-white/50'}`}
|
2025-11-19 11:45:10 +08:00
|
|
|
aria-label={`Go to slide ${index + 1}`}
|
2025-11-18 15:11:13 +08:00
|
|
|
/>
|
2025-11-18 13:30:19 +08:00
|
|
|
))}
|
2025-11-18 15:11:13 +08:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|