feat(滚动): 添加分类切换时重置虚拟滚动位置功能

添加 scrollKey 属性到 AppGrid 组件,当分类变化时自动重置滚动位置
添加相关单元测试验证滚动重置功能
This commit is contained in:
2026-04-10 16:17:38 +08:00
parent 4a2cbe1f2a
commit 1d51f38e64
3 changed files with 123 additions and 1 deletions

View File

@@ -34,6 +34,7 @@
<!-- 应用数量较多时使用虚拟滚动 -->
<RecycleScroller
v-else-if="!loading"
ref="scrollerRef"
class="scroller"
:items="gridRows"
:item-size="itemHeight"
@@ -77,16 +78,21 @@
</template>
<script setup lang="ts">
import { computed, ref, onMounted, onUnmounted } from "vue";
import { computed, ref, onMounted, onUnmounted, nextTick, watch } from "vue";
import { RecycleScroller } from "vue-virtual-scroller";
import "vue-virtual-scroller/dist/vue-virtual-scroller.css";
import AppCard from "./AppCard.vue";
import type { App } from "../global/typedefinition";
interface RecycleScrollerInstance {
$el: HTMLElement;
}
const props = defineProps<{
apps: App[];
loading: boolean;
storeFilter?: "spark" | "apm" | "both";
scrollKey?: string;
}>();
defineEmits<{
@@ -95,6 +101,7 @@ defineEmits<{
// 当前列数
const columns = ref(4);
const scrollerRef = ref<RecycleScrollerInstance | null>(null);
// 根据窗口宽度更新列数
const updateColumns = () => {
@@ -114,6 +121,19 @@ onUnmounted(() => {
window.removeEventListener("resize", updateColumns);
});
watch(
() => props.scrollKey,
async (nextKey, prevKey) => {
if (nextKey === prevKey || prevKey === undefined) return;
if (props.loading || props.apps.length <= 50) return;
await nextTick();
if (scrollerRef.value) {
scrollerRef.value.$el.scrollTop = 0;
}
},
);
// 网格列数类名
const gridColumnsClass = computed(() => {
const map: Record<number, string> = {