Skip to content

Commit 0f6acd5

Browse files
author
Taois
committed
feat: 优化滚动高度计算逻辑
1 parent 1b903b2 commit 0f6acd5

File tree

2 files changed

+71
-14
lines changed

2 files changed

+71
-14
lines changed

dashboard/src/components/SearchResults.vue

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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
});

dashboard/src/components/VideoGrid.vue

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,42 @@ const updateScrollAreaHeight = () => {
141141
containerHeight = Math.max(window.innerHeight - 120, 500);
142142
}
143143
144-
// 根据视频内容动态调整高度减值
145-
// 如果没有视频或视频很少,减去100px强制触发滚动条
146-
// 如果有足够视频内容,只减去少量padding空间
147-
const hasEnoughContent = props.videos && props.videos.length >= 17;
148-
const heightReduction = hasEnoughContent ? 4 : 100;
144+
// 改进的高度计算逻辑:
145+
// 1. 正确计算网格列数(基于容器宽度)
146+
// 2. 估算内容实际需要的高度
147+
// 3. 智能调整容器高度以确保滚动翻页正常工作
149148
150-
const newHeight = Math.max(containerHeight - footerHeight - heightReduction, 400);
151-
console.log(`视频数量: ${props.videos?.length || 0}, 内容充足: ${hasEnoughContent}, 高度减值: ${heightReduction}, 最终高度: ${newHeight}`);
149+
const videoCount = props.videos ? props.videos.length : 0;
150+
151+
// 获取容器宽度来计算列数
152+
const containerWidth = container.offsetWidth || Math.max(window.innerWidth - 240, 800); // 减去侧边栏宽度
153+
const itemWidth = 200; // 每个视频项的估算宽度
154+
const gridCols = Math.min(Math.floor(containerWidth / itemWidth), 8); // 基于宽度计算列数,最多8列
155+
156+
const estimatedItemHeight = 328; // 根据F12实际测量的高度(图片+文字)
157+
const estimatedRows = videoCount > 0 ? Math.ceil(videoCount / Math.max(gridCols, 1)) : 0;
158+
const estimatedContentHeight = estimatedRows * estimatedItemHeight + 80; // 减少padding估算
159+
160+
// 智能高度调整策略
161+
let heightReduction = 4; // 默认只减去少量padding
162+
163+
// 如果没有视频数据,使用保守的高度减值来为后续数据加载预留空间
164+
if (videoCount === 0) {
165+
heightReduction = Math.min(containerHeight * 0.3, 200); // 减去30%或200px,取较小值
166+
console.log(`无视频数据,使用保守高度减值: ${heightReduction}px`);
167+
} else if (estimatedContentHeight < containerHeight) {
168+
// 有数据但内容不足时,需要减少容器高度以触发滚动
169+
// 策略:容器高度 = 内容高度 - 120px(确保有足够滚动空间)
170+
// 但如果这样会遮挡太多内容,则至少显示1.5行的高度
171+
const minDisplayHeight = Math.floor(estimatedItemHeight * 1.5) + 80; // 至少显示1.5行
172+
const idealHeight = estimatedContentHeight - 120; // 理想的滚动高度
173+
const targetHeight = Math.max(idealHeight, minDisplayHeight, containerHeight * 0.4); // 取最大值确保不会太小
174+
heightReduction = Math.max(containerHeight - targetHeight, 80); // 最少减去80px
175+
console.log(`内容高度不足,估算内容高度: ${estimatedContentHeight}px, 理想高度: ${idealHeight}px, 最小显示高度: ${minDisplayHeight}px, 容器高度: ${containerHeight}px, 调整高度减值: ${heightReduction}px`);
176+
}
177+
178+
const newHeight = Math.max(containerHeight - footerHeight - heightReduction, 300);
179+
console.log(`视频数量: ${videoCount}, 估算行数: ${estimatedRows}, 列数: ${gridCols}, 容器宽度: ${containerWidth}px, 最终高度: ${newHeight}px`);
152180
scrollAreaHeight.value = newHeight;
153181
}, 100); // 增加延迟确保DOM完全渲染
154182
});

0 commit comments

Comments
 (0)