2025-11-18 17:42:33 +08:00
|
|
|
|
// src/components/CarouselDemo.tsx
|
2025-11-18 15:11:13 +08:00
|
|
|
|
import * as React from "react";
|
2025-11-18 20:45:54 +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-18 20:45:54 +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 17:01:45 +08:00
|
|
|
|
import FireNewsList from "./FireNewsList";
|
2025-11-18 13:30:19 +08:00
|
|
|
|
|
|
|
|
|
|
export function CarouselDemo() {
|
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-18 17:40:37 +08:00
|
|
|
|
const totalSlides = 6;
|
2025-11-18 15:11:13 +08:00
|
|
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
|
|
if (!api) return;
|
|
|
|
|
|
|
|
|
|
|
|
setCount(api.scrollSnapList().length);
|
2025-11-18 20:45:54 +08:00
|
|
|
|
setCurrent(api.selectedScrollSnap());//获取当前选中的轮播项的索引
|
2025-11-18 15:11:13 +08:00
|
|
|
|
api.on("select", () => {
|
|
|
|
|
|
setCurrent(api.selectedScrollSnap());
|
|
|
|
|
|
});
|
2025-11-18 20:45:54 +08:00
|
|
|
|
}, [api]);//当api改变时,执行此函数 水平居中 循环 自动播放 回传状态
|
2025-11-18 15:11:13 +08:00
|
|
|
|
|
2025-11-18 13:30:19 +08:00
|
|
|
|
return (
|
2025-11-18 17:40:37 +08:00
|
|
|
|
<div className="relative w-full max-w-4xl mx-auto">
|
2025-11-18 15:11:13 +08:00
|
|
|
|
<Carousel
|
|
|
|
|
|
opts={{
|
|
|
|
|
|
loop: true,
|
|
|
|
|
|
}}
|
|
|
|
|
|
plugins={[
|
|
|
|
|
|
Autoplay({
|
|
|
|
|
|
delay: 3000,
|
|
|
|
|
|
stopOnInteraction: false,
|
|
|
|
|
|
}),
|
|
|
|
|
|
]}
|
|
|
|
|
|
setApi={setApi}
|
|
|
|
|
|
>
|
2025-11-18 17:40:37 +08:00
|
|
|
|
<CarouselContent className="h-full w-full">
|
|
|
|
|
|
{Array.from({ length: totalSlides }).map((_, index) => (
|
|
|
|
|
|
<CarouselItem key={index} className="w-full h-full">
|
2025-11-18 15:11:13 +08:00
|
|
|
|
<div className="p-1">
|
|
|
|
|
|
<Card>
|
2025-11-18 17:40:37 +08:00
|
|
|
|
<CardContent className="flex aspect-square items-center justify-center ">
|
|
|
|
|
|
<div
|
|
|
|
|
|
className="w-full h-full"
|
|
|
|
|
|
style={{
|
2025-11-18 20:45:54 +08:00
|
|
|
|
backgroundImage: "url('/app/images/header.png')",//背景图片可修改
|
2025-11-18 17:40:37 +08:00
|
|
|
|
backgroundSize: '100% 100%',
|
|
|
|
|
|
}}
|
|
|
|
|
|
>
|
|
|
|
|
|
</div>
|
2025-11-18 15:11:13 +08:00
|
|
|
|
</CardContent>
|
|
|
|
|
|
</Card>
|
|
|
|
|
|
</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-18 17:42:33 +08:00
|
|
|
|
|
2025-11-18 15:11:13 +08:00
|
|
|
|
{/* 分页指示器 - 右下角 */}
|
|
|
|
|
|
<div className="absolute bottom-4 right-4 flex gap-2 z-10">
|
|
|
|
|
|
{Array.from({ length: count }).map((_, index) => (
|
|
|
|
|
|
<button
|
|
|
|
|
|
key={index}
|
|
|
|
|
|
onClick={() => api?.scrollTo(index)}
|
|
|
|
|
|
className={`h-2 w-2 rounded-full transition-colors ${
|
|
|
|
|
|
index === current ? "bg-black" : "bg-black/50"
|
|
|
|
|
|
}`}
|
|
|
|
|
|
/>
|
2025-11-18 13:30:19 +08:00
|
|
|
|
))}
|
2025-11-18 15:11:13 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|