"use client"; import React from 'react'; import { Swiper, SwiperSlide } from 'swiper/react'; import { Autoplay, EffectCoverflow, EffectCards } from 'swiper/modules'; import type { Swiper as SwiperType } from 'swiper/types'; import { useDeviceType } from '@/hooks/useDeviceType'; import 'swiper/css'; import 'swiper/css/effect-coverflow'; import 'swiper/css/effect-cards'; /** 默认视频列表(来自 home-page2.tsx 中的数组) */ const DEFAULT_VIDEOS: string[] = [ 'https://cdn.qikongjian.com/1756474023656_60twk5.mp4', 'https://cdn.qikongjian.com/1756474023644_14n7is.mp4', 'https://cdn.qikongjian.com/1756474023648_kocq6z.mp4', 'https://cdn.qikongjian.com/1756474023657_w10boo.mp4', 'https://cdn.qikongjian.com/1756474023657_nf8799.mp4', 'https://cdn.qikongjian.com/1756474230992_vw0ubf.mp4', ]; export interface VideoCoverflowProps { title?: string; subtitle?: string; /** 视频地址数组 */ videos?: string[]; /** 自动播放间隔(毫秒) */ autoplayDelay?: number; } /** * 使用 Swiper 的 Coverflow 效果展示视频,自动滚动并仅播放当前居中的视频。 * @param {VideoCoverflowProps} props - 组件属性 * @returns {JSX.Element} - 组件节点 */ const VideoCoverflow: React.FC = ({ title = '', subtitle = '', videos = DEFAULT_VIDEOS, autoplayDelay = 4500, }) => { const swiperRef = React.useRef(null); const videoRefs = React.useRef>({}); const { isMobile } = useDeviceType(); const [activeIndex, setActiveIndex] = React.useState(0); const playActive = React.useCallback((activeIndex: number) => { Object.entries(videoRefs.current).forEach(([key, el]) => { const video = el as HTMLVideoElement | null; if (!video) return; const index = Number(key); if (index === activeIndex) { // 尝试播放当前居中视频 video.play().catch(() => {}); } else { // 暂停其他视频,重置到起点以减少解码负担 video.pause(); try { video.currentTime = 0; } catch {} } }); }, []); const handleAfterInit = React.useCallback((sw: SwiperType) => { swiperRef.current = sw; const idx = sw.realIndex ?? sw.activeIndex ?? 0; setActiveIndex(idx); playActive(idx); }, [playActive]); const handleSlideChange = React.useCallback((sw: SwiperType) => { const idx = sw.realIndex ?? sw.activeIndex ?? 0; setActiveIndex(idx); playActive(idx); }, [playActive]); return (

{title}

{subtitle}

{videos.map((src, index) => (
))}
); }; export default React.memo(VideoCoverflow);