@@ -149,24 +149,42 @@ const totalHeight = computed(() => logHeight * logCount.value);
149
149
const containerHeight = ref (500 );
150
150
const visibleCount = computed (() => Math .ceil (containerHeight .value / logHeight ));
151
151
const startIndex = ref (0 );
152
+ const lastScrollTop = ref (0 );
152
153
153
154
const visibleLogs = computed (() => {
154
155
return logs .value .slice (startIndex .value , startIndex .value + visibleCount .value );
155
156
});
156
157
158
+ let scrollTimer: ReturnType <typeof setTimeout > | null = null ;
159
+
157
160
const onScroll = () => {
158
- if (logContainer .value ) {
159
- const scrollTop = logContainer .value .scrollTop ;
160
- if (scrollTop == 0 ) {
161
+ if (! logContainer .value || isLoading .value ) return ;
162
+
163
+ const scrollTop = logContainer .value .scrollTop ;
164
+ const newStartIndex = Math .max (0 , Math .floor (scrollTop / logHeight ));
165
+
166
+ if (scrollTimer ) {
167
+ clearTimeout (scrollTimer );
168
+ }
169
+
170
+ startIndex .value = newStartIndex ;
171
+
172
+ scrollTimer = setTimeout (() => {
173
+ if (scrollTop <= logHeight && minPage .value > 1 ) {
174
+ const currentScrollTop = scrollTop ;
161
175
readReq .page = minPage .value - 1 ;
162
- if (readReq .page < 1 ) {
163
- return ;
176
+ if (readReq .page >= 1 ) {
177
+ minPage .value = readReq .page ;
178
+ lastScrollTop .value = currentScrollTop ;
179
+ getContent (true );
164
180
}
165
- minPage .value = readReq .page ;
166
- getContent (true );
167
181
}
168
- startIndex .value = Math .floor (scrollTop / logHeight );
169
- }
182
+
183
+ if (! tailLog .value && scrollTop + containerHeight .value >= totalHeight .value - logHeight && maxPage .value > 1 ) {
184
+ readReq .page = maxPage .value ;
185
+ getContent (false );
186
+ }
187
+ }, 50 );
170
188
};
171
189
172
190
const changeLoading = () => {
@@ -276,11 +294,19 @@ const getContent = async (pre: boolean) => {
276
294
}
277
295
278
296
nextTick (() => {
279
- if (pre ) {
280
- logContainer .value .scrollTop = 2000 ;
281
- } else {
282
- logContainer .value .scrollTop = totalHeight .value ;
283
- containerHeight .value = logContainer .value .getBoundingClientRect ().height ;
297
+ if (logContainer .value ) {
298
+ if (pre ) {
299
+ if (! end .value ) {
300
+ const addedLines = newLogs .length ;
301
+ const newScrollPosition = lastScrollTop .value + addedLines * logHeight ;
302
+ logContainer .value .scrollTop = newScrollPosition ;
303
+ startIndex .value = Math .max (0 , Math .floor (newScrollPosition / logHeight ));
304
+ }
305
+ } else {
306
+ logContainer .value .scrollTop = totalHeight .value ;
307
+ containerHeight .value = logContainer .value .getBoundingClientRect ().height ;
308
+ startIndex .value = Math .max (0 , logs .value .length - visibleCount .value );
309
+ }
284
310
}
285
311
});
286
312
}
0 commit comments