Ben Lin
2024-06-18 ebbd788fbb2c0b45d4473798efc57eec8ba74a25
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<template>
  <PageWrapper class="virtual-scroll-demo">
    <Divider>基础滚动示例</Divider>
    <div class="text-center mb-4">
      <a-button @click="vScrollRef?.scrollToTop()">滚动到顶部</a-button>
      <a-button @click="vScrollRef?.scrollToBottom()" class="mx-2">滚动到底部</a-button>
      <a-button @click="vScrollRef?.scrollToItem(scrollToItemIndex)"
        >滚动到第
        <input-number
          v-model:value="scrollToItemIndex"
          class="!w-60px mx-1"
          :min="1"
          :max="data.length"
          :precision="0"
          size="small"
          :controls="false"
          @keydown.enter="vScrollRef?.scrollToItem(scrollToItemIndex)"
        />
        条
      </a-button>
    </div>
    <div class="virtual-scroll-demo-wrap">
      <VScroll :itemHeight="41" :items="data" :height="300" :width="300" ref="vScrollRef">
        <template #default="{ item }">
          <div class="virtual-scroll-demo__item">
            {{ item.title }}
          </div>
        </template>
      </VScroll>
    </div>
 
    <Divider>即使不可见,也预先加载50条数据,防止空白</Divider>
    <div class="virtual-scroll-demo-wrap">
      <VScroll :itemHeight="41" :items="data" :height="300" :width="300" :bench="50">
        <template #default="{ item }">
          <div class="virtual-scroll-demo__item">
            {{ item.title }}
          </div>
        </template>
      </VScroll>
    </div>
  </PageWrapper>
</template>
<script lang="ts" setup>
  import { PageWrapper } from '@/components/Page';
  import { VScroll } from '@/components/VirtualScroll';
  import { Divider, InputNumber } from 'ant-design-vue';
  import { ref } from 'vue';
 
  const vScrollRef = ref<typeof VScroll>();
  const scrollToItemIndex = ref(1000);
 
  const data = (() => {
    const arr: any[] = [];
    for (let index = 1; index < 20000; index++) {
      arr.push({
        title: '列表项' + index,
      });
    }
    return arr;
  })();
</script>
<style lang="less" scoped>
  .virtual-scroll-demo {
    &-wrap {
      display: flex;
      justify-content: center;
      margin: 0 30%;
      background-color: @component-background;
    }
 
    &__item {
      height: 40px;
      padding: 0 20px;
      border-bottom: 1px solid @border-color-base;
      line-height: 40px;
    }
  }
</style>