Summary of uniapp project practice (17) to achieve scrolling bottom loading

Introduction: During the development process of daily testing, we often encounter situations where the page needs to render a large amount of data. At this time, we need to use the rolling loading function. The method is summarized below.

Table of contents

  • Principle analysis
  • Practical exercises
  • Case display

Principle analysis

Use @scrolltoloweran event to listen for scrolling to the bottom and then loading the next page of data.

Practical exercises

template page

<scroll-view
  :scroll-y="true"
  class="block-main block-two-level block-pad"
  @scrolltolower="scrollBottom">
  <view class="scroll-ls" v-for="(item, index) in scrollInfo.list" :key="index"> {
   
   { item }} </view>
  <uni-load-more v-if="scrollInfo.list.length" :status="scrollInfo.loading"></uni-load-more>
</scroll-view>

Style writing

.scroll-ls {
  margin-top: 20rpx;
  padding: 50rpx 0;
  text-align: center;
  background: $f8;
}

Script usage

  • Define data
// 滚动列表
const scrollInfo = reactive({
    
    
  originList: [
    1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
    27, 28,
  ],
  list: [],
  pageInfo: {
    
    
    page: 1,
    size: 8,
    pages: 0,
  },
  loading: "more",
});
  • method call
// 滚动到底部
function scrollBottom() {
    
    
  console.log("滚动到底部!");
  if (scrollInfo.pageInfo.page < scrollInfo.pageInfo.pages) {
    
    
    scrollInfo.pageInfo.page++;
    scrollInfo.loading = "loading";
    getList();
  } else {
    
    
    scrollInfo.loading = "noMore";
  }
}
  • Get list
// 获取列表
function getList() {
    
    
  if (scrollInfo.pageInfo.page <= 1) {
    
    
    show.value = true;
  }
  let data = proxy.$apis.utils.splitData(scrollInfo.originList, 8);
  scrollInfo.pageInfo.pages = data.pages;
  setTimeout(() => {
    
    
    if (scrollInfo.pageInfo.page <= 1) {
    
    
      scrollInfo.list = data.list[scrollInfo.pageInfo.page - 1];
      setTimeout(() => {
    
    
        show.value = false;
      }, 500);
    } else {
    
    
      scrollInfo.list = [...scrollInfo.list, ...data.list[scrollInfo.pageInfo.page - 1]];
    }
    scrollInfo.loading = scrollInfo.pageInfo.page < scrollInfo.pageInfo.pages ? "more" : "noMore";
  }, 1000);
}

Case display

  • h5 end effect
    Insert image description here

  • Mini program effect
    Insert image description here

  • APP side effect
    Insert image description here

at last

The above is the main content to achieve scrolling bottom loading. If there are any shortcomings, please correct me.

Guess you like

Origin blog.csdn.net/fed_guanqi/article/details/132996830