news/app/components/Carousel.tsx

137 lines
4.9 KiB
TypeScript
Raw Normal View History

2025-11-20 08:12:41 +08:00
// 引入 React 核心库
2025-11-18 15:11:13 +08:00
import * as React from "react";
2025-11-20 08:12:41 +08:00
// 引入 Embla Carousel 的自动播放插件
2025-11-19 11:45:10 +08:00
import Autoplay from "embla-carousel-autoplay";
2025-11-18 13:30:19 +08:00
2025-11-20 08:12:41 +08:00
// 引入自定义 UI 组件Card 和 CardContent通常用于内容容器
2025-11-18 15:29:20 +08:00
import { Card, CardContent } from "@/ui/card";
2025-11-20 08:12:41 +08:00
// 引入自定义轮播组件及其子组件和类型定义
2025-11-18 13:30:19 +08:00
import {
2025-11-19 11:45:10 +08:00
Carousel,
CarouselContent,
CarouselItem,
2025-11-20 08:12:41 +08:00
type CarouselApi, // Embla 轮播实例的类型定义
2025-11-18 15:29:20 +08:00
} from "@/ui/carousel";
2025-11-18 19:47:03 +08:00
2025-11-20 08:12:41 +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:12:41 +08:00
// 定义组件接收的 props 接口,提供类型安全
2025-11-20 08:00:40 +08:00
export interface CarouselDemoProps {
2025-11-20 08:12:41 +08:00
// 分页指示器位置:'left' 表示左下角,'right' 表示右下角,默认为 'right'
paginationPosition?: "left" | "right";
2025-11-20 10:54:57 +08:00
2025-11-20 08:12:41 +08:00
// 分页指示器样式:'dot' 为圆形小点,'bar' 为横向小条,默认为 'dot'
paginationStyle?: "dot" | "bar";
2025-11-20 08:00:40 +08:00
}
2025-11-20 08:12:41 +08:00
// 导出 CarouselDemo 组件,接收两个可选 props并设置默认值
2025-11-20 08:00:40 +08:00
export function CarouselDemo({
paginationPosition = "right", // 默认右下角
paginationStyle = "dot", // 默认圆形指示器
2025-11-20 08:00:40 +08:00
}: CarouselDemoProps) {
2025-11-21 11:39:58 +08:00
// 轮播实例的引用 当前激活的幻灯片索引 总共的幻灯片数量 图片总数
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-20 17:52:29 +08:00
// 监听 api ,初始化轮播状态并绑定 select 事件 获取幻灯片总数 设置当前选中的 snap 索引 用户手动滑动或自动播放时触发 更新当前激活项
2025-11-18 15:11:13 +08:00
React.useEffect(() => {
if (!api) return;
2025-11-18 15:11:13 +08:00
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-18 15:11:13 +08:00
});
2025-11-20 08:12:41 +08:00
}, [api]); // 仅当 api 发生变化时重新执行
2025-11-18 15:11:13 +08:00
2025-11-20 08:12:41 +08:00
// 渲染组件
2025-11-18 13:30:19 +08:00
return (
2025-11-20 08:12:41 +08:00
// 外层容器:相对定位,占满父容器宽高
2025-11-19 11:45:10 +08:00
<div className="relative w-full h-full">
2025-11-20 08:12:41 +08:00
{/* 轮播主容器 */}
2025-11-18 15:11:13 +08:00
<Carousel
2025-11-20 08:12:41 +08:00
opts={{
loop: true,
2025-11-20 08:12:41 +08:00
}}
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
}),
]}
2025-11-20 17:52:29 +08:00
setApi={setApi} // 保存
className="w-full"
2025-11-18 15:11:13 +08:00
>
2025-11-20 17:52:29 +08:00
{/*
*/}
<CarouselContent className=" w-full -ml-0 h-full">
2025-11-20 08:12:41 +08:00
{/* 遍历图片数组,为每张图创建一个轮播项 */}
2025-11-20 08:00:40 +08:00
{imageUrls.map((src, index) => (
<CarouselItem key={index} className="w-full pl-0">
2025-11-20 17:52:29 +08:00
{/*
*/}
2025-11-21 11:39:58 +08:00
<div className={`p-0 w-full `}>
2025-11-20 10:50:06 +08:00
<CardContent className=" p-0 w-full m-0">
2025-11-20 17:52:29 +08:00
<div className="relative w-full aspect-video ">
2025-11-20 10:50:06 +08:00
<img
src={src}
alt={`Slide ${index + 1}`} // 无障碍访问描述
className="absolute inset-0 w-full h-full object-fill" // 使用 object-fill 拉伸填充
loading="lazy" // 懒加载优化性能
/>
</div>
2025-11-19 16:30:50 +08:00
</CardContent>
2025-11-18 15:11:13 +08:00
</div>
</CarouselItem>
))}
</CarouselContent>
2025-11-20 08:12:41 +08:00
{/* 上一张按钮(左箭头) */}
2025-11-20 08:29:49 +08:00
{/* <CarouselPrevious className="absolute left-2 top-1/2 -translate-y-1/2 z-10" /> */}
2025-11-20 08:12:41 +08:00
{/* 下一张按钮(右箭头) */}
2025-11-20 08:29:49 +08:00
{/* <CarouselNext className="absolute right-2 top-1/2 -translate-y-1/2 z-10" /> */}
2025-11-18 15:11:13 +08:00
</Carousel>
2025-11-20 08:12:41 +08:00
{/* 分页指示器容器:绝对定位在底部 */}
2025-11-20 08:00:40 +08:00
<div
className={`absolute bottom-4 ${
paginationPosition === "left" ? "left-4" : "right-4" // 根据 prop 控制左右位置
} flex gap-2 z-10`} // 水平排列,间距 0.5rem,置于轮播图上方
2025-11-20 08:00:40 +08:00
>
2025-11-20 08:12:41 +08:00
{/* 动态生成指示器按钮 */}
2025-11-18 15:11:13 +08:00
{Array.from({ length: count }).map((_, index) => (
<button
key={index}
2025-11-20 08:12:41 +08:00
onClick={() => api?.scrollTo(index)} // 点击跳转到对应幻灯片
2025-11-19 16:30:50 +08:00
className={`transition-colors ${
2025-11-20 08:12:41 +08:00
// 根据 paginationStyle 决定形状
paginationStyle === "dot"
? "h-2 w-2 rounded-full" // 圆形:宽高相等 + 全圆角
: "h-1 w-6 rounded" // 块状
} ${
index === current
? "bg-white" // 当前项为纯白色
: "bg-white/50" // 非当前项为半透明白色
}`}
2025-11-20 08:12:41 +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>
);
}