Element-UI 滾動加載 InfiniteScroll 無限滾動

把加載的數(shù)據(jù)存放到一個有滾動條的盒子里,因為這個功能主要是根據(jù)滾動條滑到最底部進行加載,反之就會出現(xiàn)一直加載,直到數(shù)據(jù)加載完畢為止。infinite-scroll-immediate 是否立即執(zhí)行加載方法,以防初始狀態(tài)下內容無法撐滿容器。默認為是,記得選否。

(vant 滾動加載+下拉刷新,改變篩選條件時重新渲染數(shù)據(jù),容易出現(xiàn)重復加載,:immediate-check="false"是否在初始化時立即執(zhí)行滾動位置檢查關掉,手動onLoad();記得onLoad的時候把this.loading=false;this.finished=false;)

<template>
  <div id="box">
    <div class="box" v-infinite-scroll="load" infinite-scroll-disabled="disabled" >
        <ul class="list" >
          <li v-for="(i,index) in list" class="list-item" :key="index">{{ i.noticeTitle }}</li>
        </ul>
        <p v-if="loading" style="margin-top:10px;" class="loading">
          <span></span>
        </p>
        <p v-if="noMore" style="margin-top:10px;font-size:13px;color:#ccc">沒有更多了</p>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      count: 0,//起始頁數(shù)值為0
      loading: false,
      totalPages: "",//取后端返回內容的總頁數(shù)
      list: [] //后端返回的數(shù)組
    };
  },
  computed: {
    noMore() {
      //當起始頁數(shù)大于總頁數(shù)時停止加載
      return this.count >= this.totalPages - 1;
    },
    disabled() {
      return this.loading || this.noMore;
    }
  },
  created() {
    this.getMessage();
  },
  methods: {
    load() {
      //滑到底部時進行加載
      this.loading = true;
      setTimeout(() => {
        this.count += 1; //頁數(shù)+1
        this.getMessage(); //調用接口,此時頁數(shù)+1,查詢下一頁數(shù)據(jù)
      }, 2000);
    },
    getMessage() {
      let params = {
        pageNumber: this.count,
        pageSize: 10 //每頁查詢條數(shù)
      };
      this.$axios
        .post(
          "https://接口",
          params
        )
        .then(res => {
          console.log(res);
          this.list = this.list.concat(res.data.body.content); //因為每次后端返回的都是數(shù)組,所以這邊把數(shù)組拼接到一起
          this.totalPages = res.data.body.totalPages;
          this.loading = false;
        })
        .catch(err => {
          console.log(err);
        });
    }
  }
};
</script>

<style scoped>
#box{
    width: 100%;
  height: 100%;
  position: absolute;
  overflow-y: auto;
}
.box {
  width: 100%;
   margin:  0 auto;
}
.list {
  padding: 0;
  font-size: 14px;
}
.list-item {
  width: 100%;
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  list-style: none;
  padding: 0 1rem;
  box-sizing: border-box;
  height: 70px;
  line-height: 70px;
  border-bottom: 1px solid #e7e7e7;
}
.loading span {
  display: inline-block;
  width: 20px;
  height: 20px;
  border: 2px solid #409eff;
  border-left: transparent;
  animation: zhuan 0.5s linear infinite;
  border-radius: 50%;
}
@keyframes zhuan {
  0% {
    transform: rotate(0);
  }
  100% {
    transform: rotate(360deg);
  }
}
</style>

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內容