Skip to content

virtual-list(虚拟列表)

基础用法

示例:virtual-list1.png

代码示例:

vue
<template>
  <div style="height: 650px">
    <virtual-list
      style="height: 100%; overflow-y: auto"
      :data-key="'id'"
      :data-sources="list"
      :data-component="itemComponent"
      :keeps="20"
      :extra-props="{
        address,
        basisInfo,
      }"
      @tobottom="listToBottom"
    />
  </div>
</template>

<script>
import itemComponent from "./itemComponent";
export default {
  data() {
    return {
      itemComponent,
      list: [],
      address: { province: "江苏", city: "南京" },
      basisInfo: [{ age: 20 }],
    };
  },
  components: {},
  methods: {
    content() {
      for (let i = 0; i < 10000; i++) {
        const obj = { id: i, name: `人员${i}` };
        this.list.push(obj);
      }
    },
    listToBottom() {
      console.log("到底了哦");
    },
  },
  mounted() {
    this.content();
  },
};
</script>
vue
<template>
  <div>
    编号:{{ index }} <br />
    姓名:{{ source.name }}<br />
    年龄:{{ basisInfo[0].age }}<br />
    地址:{{ address.province }}-{{ address.city }}
    <hr />
  </div>
</template>

<script>
export default {
  name: "item-component",
  props: {
    // index of current item
    index: {
      type: Number,
    },
    source: {
      type: Object,
      default() {
        return {};
      },
    },
    address: {
      type: Object,
      default() {
        return {};
      },
    },
    basisInfo: {
      type: Array,
      default() {
        return [];
      },
    },
  },
  mounted() {},
};
</script>

属性:

参数说明类型可选值默认值
dataKey唯一键来自每个数据对象中的 data-sources。或者使用每个 data-sources 调用的函数并返回它们的唯一键。它的值在 data-sources 中必须是唯一的,用于标识节点大小。(必填)[String, Function]--
dataSources列表构建的源数组,每个数组数据必须是一个对象,并且有一个唯一的 key 或 generate for data-key 属性。(必填)Array--
dataComponent由 vue 创建 / 声明的渲染项组件,将使用 data-sources 中的数据对象作为渲染 prop 并命名为:source。(必填)[Object, Function]--
keeps虚拟列表在真实 dom 中保持渲染的节点数量。Number-30
extraProps额外的参数(不在 data-sources 中的)分配给节点组件。注意:index 和 source 都已被占用。Object--
estimateSize每个节点的预计尺寸,如果更接近平均尺寸,滚动条长度看起来更准确。建议:分配自己计算的平均值。Number-50
direction滚动方向,可用值为 vertical 和 horizontal。String'vertical','horizontal''vertical'
start设置滚动位置停留开始索引。Number-0
offset设置滚动位置保持偏移。Number-0
topThreshold触发 totop 事件的阈值,注意:多次调用Number-0
bottomThreshold触发 tobottom 事件的阈值,注意:多次调用Number-0
pageMode设置虚拟列表使用全局文档滚动列表。Boolean-false
rootTag根元素标记名称。String-'div'
wrapTag包裹元素(role = item)标签名称。String-'div'
wrapClass包裹元素类名。String-''
wrapStyle节点包裹元素内联样式。Object--
itemTag包裹元素(role = item)标签名称。String-'div'
itemClass包裹元素类名。String-''
itemClassAdd可将额外的类(字符串)返回到节点包裹元素参数(索引)的函数。Function--
itemStyle节点包裹元素内联样式。Object--
headerTag对于使用头槽,头槽包裹元素(role = header)标签名称。String-'div'
headerClass对于使用头槽,头槽包裹元素类名。String-''
headerStyle对于使用头槽,头槽包裹元素内联样式。Object--
footerTag对于使用页脚槽,页脚槽包裹元素(role = footer)标签名称。String-'div'
footerClass对于使用页脚槽,页脚槽包裹元素类名。String-''
footerStyle用于使用页脚槽、页脚槽包裹元素内联样式。Object--
itemScopedSlots节点组件的 $scopedSlots。Object--

事件:

事件名说明参数参数说明
resized调整项目大小时发出 (mounted),回调参数 (id, size)。id, size-
scroll滚动时发出,回调参数(event, range)evt, range-
totop滚动到顶部或左侧时发出,无参数。--
tobottom滚动到底部或右侧时发出,无参数。--

方法:

方法名说明参数参数说明
getSize通过 id 获取指定的 item 大小(来自 data-key value)。id-
getSizes获取存储(渲染)节点的总数。--
getOffset获取当前滚动偏移量。--
getClientSize获取包裹元素视口大小(宽度或高度)。--
getScrollSize获取所有滚动大小(scrollHeight 或 scrollWidth)。--
scrollToOffset手动将滚动位置设置为指定的偏移量。offset-
scrollToIndex手动将滚动位置设置为指定的索引。index-
scrollToBottom手动将滚动位置设置为底部。--
updatePageModeFront当使用页面模式和虚拟列表根元素 offsetTop 或 offsetLeft 变化时,需要手动调用该方法。--
reset将所有状态重置回初始状态。--