# InfiniteScroll 无限滚动

滚动至底部时,加载更多数据。

# 基础用法

在要实现滚动加载的列表上上添加v-infinite-scroll,并赋值相应的加载方法,可实现滚动到底部时自动执行加载方法。

    <template>
      <ul class="infinite-list" v-infinite-scroll="load" style="overflow: auto">
        <li v-for="i in count" class="infinite-list-item" :key="i">{{ i }}</li>
      </ul>
    </template>
    
    <script>
    export default {
      data() {
        return {
          count: 0
        }
      },
      methods: {
        load() {
          this.count += 2
        }
      }
    }
    </script>
    <style scoped lang="scss">
    .infinite-list {
      height: 300px;
      padding: 0;
      margin: 0;
      list-style: none;
    
      .infinite-list-item {
        display: flex;
        align-items: center;
        justify-content: center;
        height: 50px;
        background: #e8f3fe;
        margin: 10px;
        color: lighten(#1989fa, 20%);
        & + .list-item {
          margin-top: 10px;
        }
      }
    }
    </style>
    
    显示代码

    # 禁用加载

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    <template>
      <div class="infinite-list-wrapper" style="overflow: auto">
        <ul class="list" v-infinite-scroll="load" infinite-scroll-disabled="disabled">
          <li v-for="i in count" class="list-item" :key="i">{{ i }}</li>
        </ul>
        <p v-if="loading">加载中...</p>
        <p v-if="noMore">没有更多了</p>
      </div>
    </template>
    
    <script>
    export default {
      data() {
        return {
          count: 10,
          loading: false
        }
      },
      computed: {
        noMore() {
          return this.count >= 20
        },
        disabled() {
          return this.loading || this.noMore
        }
      },
      methods: {
        load() {
          this.loading = true
          setTimeout(() => {
            this.count += 2
            this.loading = false
          }, 2000)
        }
      }
    }
    </script>
    <style scoped lang="scss">
    .infinite-list-wrapper {
      height: 300px;
      text-align: center;
    
      .list {
        padding: 0;
        margin: 0;
        list-style: none;
      }
    
      .list-item {
        display: flex;
        align-items: center;
        justify-content: center;
        height: 50px;
        background: #fff6f6;
        color: #ff8484;
        & + .list-item {
          margin-top: 10px;
        }
      }
    }
    </style>
    
    显示代码

    # Attributes

    Attribute Description Type Accepted values Default
    infinite-scroll-disabled is disabled boolean - false
    infinite-scroll-delay throttle delay (ms) number - 200
    infinite-scroll-distance trigger distance (px) number - 0
    infinite-scroll-immediate Whether to execute the loading method immediately, in case the content cannot be filled up in the initial state. boolean - true