# infinity

The infinity plugin provides BetterScroll with unlimited scrolling capabilities. If you have a large amount of list data to render, you can use the infinity plugin, in which BetterScroll will only render a certain number of DOM elements, so that the page will continue to scroll smoothly when a large amount of data.

Note: Unless you have a lot of data rendering needs, use coreScroll.

# Install

npm install @better-scroll/infinity --save

// or

yarn add @better-scroll/infinity

# Usage

First, install the plugin via the static method BScroll.use()

import BScroll from '@better-scroll/core'
import InfinityScroll from '@better-scroll/infinity'

BScroll.use(InfinityScroll)

Then, To instantiate BetterScroll, you need to pass the related configuration item infinity:

new BScroll('.bs-wrapper', {
  scrollY: true,
  infinity: {
    fetch(count) {
      // Fetch data that is larger than count, the function is asynchronous, and it needs to return a Promise.。
      // After you have successfully fetch the data, you need resolve an array of data (or resolve Promise).
      // Each element of the array is list data, which will be rendered when the render method executes。
      // If there is no data, you can resolve (false) to tell the infinite scroll list that there is no more data。
    }
    render(item, div?: HTMLElement) {
      // Rendering each element node, item is data from fetch function
      // div is an element which is recycled from document or undefined
      // The function needs to return to a html element.
    },
    createTombstone() {
      // Must return a tombstone DOM node.
    }
  }
})

Note

fetch, render, createTombstone must be implemented in accordance with the comments as above, otherwise an internal error will be reported.

The plugin relies on Promise internally. If the browser does not support it, the Promise Polyfill is required.

# Demo

<template>
  <div class="infinity">
    <div class="template">
      <li ref="message" class="infinity-item">
        <img class="infinity-avatar" width="48" height="48">
        <div class="infinity-bubble">
          <p></p>
          <img width="300" height="300">
          <div class="infinity-meta">
            <time class="infinity-posted-date"></time>
          </div>
        </div>
      </li>
      <li ref="tombstone" class="infinity-item tombstone">
        <img class="infinity-avatar" width="48" height="48" :src="require('./image/unknown.jpg')">
        <div class="infinity-bubble">
          <p></p>
          <p></p>
          <p></p>
          <div class="infinity-meta">
            <time class="infinity-posted-date"></time>
          </div>
        </div>
      </li>
    </div>
    <div ref="chat" class="infinity-timeline">
      <ul>
      </ul>
    </div>
  </div>
</template>