news/app/components/AutoCarousel.tsx

95 lines
4.0 KiB
TypeScript
Raw Permalink Normal View History

2025-11-21 11:39:58 +08:00
// 引入 Embla Carousel 的自动播放插件
2025-11-18 19:45:46 +08:00
import Autoplay from "embla-carousel-autoplay";
2025-11-21 11:39:58 +08:00
// 引入自定义 UI 组件Card 和 CardContent通常用于内容容器
2025-11-19 17:03:34 +08:00
import { Card, CardContent } from "@/ui/card";
2025-11-21 11:39:58 +08:00
// 引入自定义轮播组件及其子组件和类型定义
2025-11-18 19:45:46 +08:00
import {
Carousel,
CarouselContent,
CarouselItem,
2025-11-21 11:39:58 +08:00
type CarouselApi, // Embla 轮播实例的类型定义
2025-11-20 17:52:29 +08:00
} from "@/ui/carousel";
2025-11-21 11:39:58 +08:00
import * as React from "react";
2025-11-18 19:45:46 +08:00
2025-11-20 17:52:29 +08:00
// 定义轮播图中要展示的图片资源路径数组
// 包含轮播主图carousel-*.jpg以及额外的静态资源如 book1.png、header.png 等)
2025-11-18 19:45:46 +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-20 17:52:29 +08:00
"/images/book1.png", // 可能是书籍封面
"/images/header.png", // 可能是顶部 Banner 图
"/images/jcdt.png", // 可能是“基层动态”等模块图标
2025-11-18 19:45:46 +08:00
];
2025-11-20 17:52:29 +08:00
// 导出名为 AutoCarouselDemo 的函数式组件
2025-11-18 19:45:46 +08:00
export function AutoCarouselDemo() {
2025-11-21 11:39:58 +08:00
const [api, setApi] = React.useState<CarouselApi>();
const [selectedIndex, setSelectedIndex] = React.useState(0);
const [current, setCurrent] = React.useState(0);
React.useEffect(() => {
if (!api) return;
setCurrent(api.selectedScrollSnap());
api.on("select", () => {
setCurrent(api.selectedScrollSnap());
});
}, [api]);
2025-11-18 19:45:46 +08:00
return (
2025-11-20 17:52:29 +08:00
// 外层容器相对定位水平居中上下留有足够间距mt-30 ≈ 120px, mb-20 ≈ 80px
2025-11-20 08:00:40 +08:00
<div className="relative w-full mx-auto mt-30 mb-20">
2025-11-19 17:03:34 +08:00
2025-11-20 17:52:29 +08:00
{/* 轮播主组件 */}
2025-11-18 19:45:46 +08:00
<Carousel
2025-11-19 17:03:34 +08:00
opts={{
2025-11-20 17:52:29 +08:00
loop: true, // 启用无限循环:最后一张后自动回到第一张
align: "start", // 对齐方式设为“起始对齐”,确保首项紧贴左侧(适用于非全屏单图场景)
2025-11-19 17:03:34 +08:00
}}
2025-11-18 19:45:46 +08:00
plugins={[
2025-11-20 17:52:29 +08:00
// 配置自动播放插件:
2025-11-19 17:03:34 +08:00
Autoplay({
2025-11-20 17:52:29 +08:00
delay: 3000, // 每 3 秒自动切换一次
stopOnInteraction: false, // 用户点击或拖拽后不停止自动播放(持续轮播)
2025-11-19 17:03:34 +08:00
}),
2025-11-18 19:45:46 +08:00
]}
2025-11-20 17:52:29 +08:00
className="w-full" // 轮播容器占满父级宽度
2025-11-18 19:45:46 +08:00
>
2025-11-20 17:52:29 +08:00
{/* 轮播内容区域:添加 gap-4 实现卡片间 1rem16px间距 */}
2025-11-21 11:39:58 +08:00
<CarouselContent className="flex gap-16 h-full ">
2025-11-20 17:52:29 +08:00
{/* 遍历所有图片 URL为每张图生成一个轮播项 */}
2025-11-19 17:03:34 +08:00
{imageUrls.map((src, index) => (
<CarouselItem
key={index}
2025-11-20 17:52:29 +08:00
// 响应式宽度控制:
2025-11-21 11:39:58 +08:00
// - 默认(小屏):每个项占 1/4 宽度basis-1/4中屏md占 1/3
2025-11-20 17:52:29 +08:00
// - 中屏md占 1/3
// - 大屏lg回到 1/4适合展示 4 列
2025-11-21 11:39:58 +08:00
className="basis-1/4 md:basis-1/3 lg:basis-1/4 flex-shrink-0 relative "
2025-11-19 17:03:34 +08:00
>
2025-11-20 17:52:29 +08:00
{/* 卡片外层:添加内边距、悬停缩放动效、鼠标指针样式 */}
2025-11-21 11:39:58 +08:00
<div className="p-1 transition-transform duration-500 cursor-pointer">
2025-11-20 17:52:29 +08:00
{/* 图片容器:固定高度 h-120即 30rem = 480px隐藏溢出内容 */}
<div className="overflow-hidden border-none h-120">
{/* 使用 CardContent 包裹图片,确保与 UI 系统一致 */}
2025-11-21 11:39:58 +08:00
<CardContent className="relative w-full h-full p-0 "> {/* p-0 移除默认内边距 */}
2025-11-20 17:52:29 +08:00
{/* 实际图片元素 */}
2025-11-19 17:03:34 +08:00
<img
src={src}
2025-11-20 17:52:29 +08:00
alt={`Slide ${index + 1}`} // 无障碍访问:描述当前幻灯片
2025-11-21 11:39:58 +08:00
className="w-full h-full object-cover hover:scale-105 transition-transform duration-500" // 宽高填满容器,保持比例裁剪
2025-11-20 17:52:29 +08:00
loading="lazy" // 懒加载:提升页面初始加载性能
2025-11-19 17:03:34 +08:00
/>
</CardContent>
2025-11-19 16:30:50 +08:00
</div>
2025-11-19 17:03:34 +08:00
</div>
</CarouselItem>
))}
2025-11-18 19:45:46 +08:00
</CarouselContent>
2025-11-20 17:52:29 +08:00
2025-11-18 19:45:46 +08:00
</Carousel>
</div>
);
}