diff --git a/src/components/HeroBanner.tsx b/src/components/HeroBanner.tsx index 2275769671..ded8b1fad8 100644 --- a/src/components/HeroBanner.tsx +++ b/src/components/HeroBanner.tsx @@ -58,14 +58,36 @@ function HeroBanner({ const refreshTrailerMutation = useRefreshTrailerUrlMutation(); const clearTrailerMutation = useClearTrailerUrlMutation(); - // 重置越界的 currentIndex + // 重置越界的 currentIndex - 立即同步重置,避免渲染崩溃 useEffect(() => { if (items.length > 0 && currentIndex >= items.length) { console.warn('[HeroBanner] currentIndex out of bounds, resetting to 0'); setCurrentIndex(0); + setVideoLoaded(false); // 重置视频加载状态 } }, [items.length, currentIndex]); + // 监听items变化,立即重置状态 + useEffect(() => { + if (items.length === 0) { + setCurrentIndex(0); + setVideoLoaded(false); + setIsTransitioning(false); + } + + // 当items变化时,停止当前视频播放,避免引用过期的item + if (videoRef.current) { + try { + videoRef.current.pause(); + videoRef.current.src = ''; + videoRef.current.load(); + } catch (error) { + console.warn('[HeroBanner] 清理视频失败:', error); + } + } + setVideoLoaded(false); + }, [items]); + // 处理图片 URL,使用代理绕过防盗链 const getProxiedImageUrl = (url: string) => { if (url?.includes('douban') || url?.includes('doubanio')) { @@ -320,7 +342,7 @@ function HeroBanner({ /> {/* 视频背景(如果启用且有预告片URL,加载完成后淡入) */} - {enableVideo && getEffectiveTrailerUrl(item) && index === currentIndex && ( + {enableVideo && getEffectiveTrailerUrl(item) && index === currentIndex && index === safeIndex && (