@@ -193,14 +193,43 @@ const updateScrollAreaHeight = () => {
193193 const searchHeader = document .querySelector (' .search-header' );
194194 const headerHeight = searchHeader ? searchHeader .offsetHeight : 60 ;
195195
196- // 根据搜索结果动态调整高度减值
197- // 如果没有结果或结果很少,减去100px强制触发滚动条
198- // 如果有足够搜索结果,只减去少量padding空间
199- const hasEnoughContent = props .videos && props .videos .length >= 17 ;
200- const heightReduction = hasEnoughContent ? 4 : 100 ;
196+ // 改进的高度计算逻辑:
197+ // 1. 正确计算网格列数(基于容器宽度)
198+ // 2. 估算内容实际需要的高度
199+ // 3. 智能调整容器高度以确保滚动翻页正常工作
201200
202- const newHeight = Math .max (containerHeight - headerHeight - heightReduction, 400 );
203- console .log (` 搜索结果数量: ${ props .videos ? .length || 0 }, 内容充足: ${hasEnoughContent}, 高度减值: ${heightReduction}, 最终高度: ${newHeight}` );
201+ const videoCount = props .videos ? props .videos .length : 0 ;
202+
203+ // 获取容器宽度来计算列数
204+ const containerWidth = container .offsetWidth || Math .max (window .innerWidth - 240 , 800 ); // 减去侧边栏宽度
205+ const itemWidth = 200 ; // 每个搜索结果项的估算宽度
206+ const gridCols = Math .min (Math .floor (containerWidth / itemWidth), 8 ); // 基于宽度计算列数,最多8列
207+
208+ const estimatedItemHeight = 328 ; // 根据F12实际测量的高度(图片+文字)
209+ const estimatedRows = videoCount > 0 ? Math .ceil (videoCount / Math .max (gridCols, 1 )) : 0 ;
210+ const estimatedContentHeight = estimatedRows * estimatedItemHeight + 80 ; // 减少padding估算
211+
212+ // 智能高度调整策略
213+ let heightReduction = 4 ; // 默认只减去少量padding
214+ const availableHeight = containerHeight - headerHeight;
215+
216+ // 如果没有搜索结果数据,使用保守的高度减值
217+ if (videoCount === 0 ) {
218+ heightReduction = Math .min (availableHeight * 0.3 , 200 ); // 减去30%或200px,取较小值
219+ console .log (` 无搜索结果数据,使用保守高度减值: ${ heightReduction} px` );
220+ } else if (estimatedContentHeight < availableHeight) {
221+ // 有数据但内容不足时,需要减少容器高度以触发滚动
222+ // 策略:容器高度 = 内容高度 - 120px(确保有足够滚动空间)
223+ // 但如果这样会遮挡太多内容,则至少显示1.5行的高度
224+ const minDisplayHeight = Math .floor (estimatedItemHeight * 1.5 ) + 80 ; // 至少显示1.5行
225+ const idealHeight = estimatedContentHeight - 120 ; // 理想的滚动高度
226+ const targetHeight = Math .max (idealHeight, minDisplayHeight, availableHeight * 0.4 ); // 取最大值确保不会太小
227+ heightReduction = Math .max (availableHeight - targetHeight, 80 ); // 最少减去80px
228+ console .log (` 搜索内容高度不足,估算内容高度: ${ estimatedContentHeight} px, 理想高度: ${ idealHeight} px, 最小显示高度: ${ minDisplayHeight} px, 可用高度: ${ availableHeight} px, 调整高度减值: ${ heightReduction} px` );
229+ }
230+
231+ const newHeight = Math .max (containerHeight - headerHeight - heightReduction, 300 );
232+ console .log (` 搜索结果数量: ${ videoCount} , 估算行数: ${ estimatedRows} , 列数: ${ gridCols} , 容器宽度: ${ containerWidth} px, 最终高度: ${ newHeight} px` );
204233 scrollAreaHeight .value = newHeight;
205234 }, 100 ); // 延迟确保DOM完全渲染
206235 });
0 commit comments