1.4.3 • Published 5 years ago

@rikishi/vue-virtual-scroll-list v1.4.3

Weekly downloads
33
License
MIT
Repository
github
Last release
5 years ago

Table of contents

Advantages

  • Items are independent.

  • Tiny and very easy to use.

  • Big data list with high performance.

Live demos

The main difference between item-mode and vfor-mode is that: item-mode make a higher performance but not very convenient to handle changing data frequently; however, vfor-mode is just the opposite.

Besides, you can also compare the experience which without using virtual-list here: without-virtual-list.

How it works

Simple usage

npm install vue-virtual-scroll-list --save

vfor-mode

All you need to care about is only data!

<template>
  <div>
    <virtual-list :size="40" :remain="8">
      <item v-for="item of items" :key="item.id" />
    </virtual-list>
  </div>
</template>
<script>
  import item from '../item.vue'
  import virtualList from 'vue-virtual-scroll-list'
  export default {
    data () {
      return {
        items: [ {id: 1}, {id: 2}, {id: 3}, ... ]
      }
    },
    components: { item, 'virtual-list': virtualList }
  }
</script>

item-mode

This mode can save a considerable amount of memory and performance. Props item, itemcount and itemprops are both required, you don't need put <item/> with a v-for directive inside virtual-list, just assign it as prop item:

<template>
  <div>
    <virtual-list :size="40" :remain="8"
      :item="item"
      :itemcount="100000"
      :itemprops="getItemprops"
    />
  </div>
</template>
<script>
  import itemComponent from '../item.vue'
  import virtualList from 'vue-virtual-scroll-list'
  export default {
    data () {
      return {
        item: itemComponent,
      }
    },
    methods: {
      getItemprops (itemIndex) {
        // <item/> will render with following data object:
        // https://vuejs.org/v2/guide/render-function.html#The-Data-Object-In-Depth
        return {
          props: itemProps,
          attrs: itemAttrs,
          ...
        }
      }
    },
    components: { 'virtual-list': virtualList }
  }
</script>

Whenever you want to change any item data from list in this mode, you need call public method forceRender() after source data change. Increase or decrease items, you need to keep itemcount and call forceRender() together.

variable height

Using variable height, props remain and size is still required. All the index variable height and scroll offset will be cached by virtual-list after the binary-search calculate, if you want to change anyone <item/> height from data, you need call public method updateVariable(index) to clear the offset cache.

If you assign variable as true, do not set inline style height inside <item/> component, you must set inline style height on <item/> component outside directly, such as:

<template>
  <div>
    <virtual-list :size="40" :remain="8" :variable="true">
      <item v-for="item of items" :key="item.id" :style="{ height: item.height + 'px' }" />
    </virtual-list>
  </div>
</template>

More use ways or getting start you can refer to these clearly demos or test suites.

Performance comparison

According to the demos above, here are lists of approximate statistics:

Build time wasted

Build amountitem-modevfor-modewithout virtual list
1,0008 ms35 ms220 ms
10,00010 ms170 ms1500 ms
100,00020 ms1300 msBrowser crash!

Total memory used

Build amountitem-modevfor-modewithout virtual list
1,00010 MB80 MB200 MB
10,00025 MB120 MB220 MB
100,00055 MB550 MBBrowser crash!

Attentions

  • Must assign the :key property on <item> component or dom frag with v-for directive.

  • Consider using box-sizing: border-box on <item> if you want absolutely correct scroll height.

Props type

Props of basic:

PropTypeRequiredDescription
sizeNumberEach list item height, in variable height, this prop just use to calculate the virtual-list outside container viewport fixed height.
remainNumberHow many items should be shown in virtual-list viewport, so size and remain determine the outside container viewport height (size × remian).

Props of performance:

PropTypeRequiredDescription
benchNumber*Default value is equal to remain, unreached items count, not show in virtual-list viewport but exist in real DOM, the larger the bench, the higher the scroll performance will achieved.
debounceNumber*It's disabled by default, milliseconds of using debounce function to ensure scroll event doesn't fire so often that it bricks browser performance.

Props of position:

PropTypeRequiredDescription
startNumber*Default value is 0, the initial scroll start index. It must be integer and in the range of list index, if invalid there will be effected as 0 or the last one.
offsetNumber*Default value is 0, the initial scroll offset. If both start and offset are assigned at initialization, start is preferred.

Props of element and class:

PropTypeRequiredDescription
rtagString*Default value is div, virtual-list root element tag name, in all cases it's style is set to display: block;
wtagString*Default value is div, virtual-list item wrapper element tag name, in all cases it's style is set to display: block;
wclassString*Default is no classname, virtual-list item wrapper element class, if assign this prop, you better not to change it's CSS box model.

Props of scroll mode:

PropTypeRequiredDescription
pagemodeBoolean*Let virtual-list scroll with window page viewport.
scrollelementHTMLElement*Let virtual-list scroll with a parent element. When pagemode is true, scrollelement is ignored.

Props of scroll event:

PropTypeRequiredDescription
totopFunction*Called when virtual-list is scrolled to top, no param.
tobottomFunction*Called when virtual-list is scrolled to bottom, no param.
onscrollFunction*Called when virtual-list is scrolling, with param: (event, data).

Props of variable height:

PropTypeRequiredDescription
variableFunction or Boolean*If assign Function, this prop is a variable height getter function which is called with param: (index) when each item is ready to be calculated; if assign Boolean, virtual-list will get each item variable height by it's inline style height automatic.

Props of item-mode:

PropTypeRequiredDescription
itemComponent*List item vue component or vNode.
itemcountNumber*List total count, you should update this prop when source data changed.
itempropsFunction*A function call when each item is going to be rendered, you can assign props or data to each item component in this function.

Public methods

Here are some usefull public methods you can call via ref:

  • forceRender(): force render virtual-list if you need or make it refresh.

  • updateVariable(index): update item height by index in variable height list.

Contributions

Welcome to improve this vue component with any issue, pull request or code review!

Changelogs

Maintain and update occasionally, for changes see release.

License

MIT License