【转载】H5页面列表的无线滚动加载(前端分页)

本代码基于Vue的架构

1.首先,要滚动的内容放在一个‘id=box’ 的div里,对div进行scroll事件的监听

<div id="box" class="midea2" @scroll="boxScroll" style="-webkit-overflow-scrolling: touch; flex=1" :style="{height: appHeight + 'px'}">
</div>
<load-more style="width: 100%; margin-top: 20px;" v-if="questionList.length !== 0" v-show="!isLogic" :tip="loadText" :show-loading="showLoad"></load-more>

参数配置:

const perpage = 10
hasMore: true, // 标记是否还有数据
loadText: '', // loading时的文字
showLoading: false, // 展示界面loading
pageNo: 1,
pageLimit: perpage,
totalCount: 0,

部分解释:

 在移动端上,在你用overflow-y:scorll属性的时候,你会发现滚动的效果很木,很慢,这时候可以使用-webkit-overflow-scrolling:touch这个属性,让滚动条产生滚动回弹的效果,就像ios原生的滚动条一样流畅。

首先,页面初始化,前端分页,把全部列表数据赋给一个变量: this.wholeList 

引入要引用的:

import _ from 'lodash'

 boxScroll监听事件:

复制代码
复制代码
      // 监听box滚动
      boxScroll (e) {
        let box = e.target
        // console.log('box is scrolling')
        if (box.scrollHeight - box.scrollTop === box.offsetHeight && box.scrollTop !== 0) {
          this.showLoad = true
          console.log('bottom~~')
          this._checkMore()
          setTimeout(this.searchMore, 700)
          // this.searchMore()
        }
      },
复制代码
复制代码

检测是否还有'下一页'的方法:

复制代码
复制代码
     _checkMore () {
        // const resident = data.data
        // if (resident.myJoinList.length < perpage || (resident.paginator.pageNo - 1) * perpage + resident.myJoinList.length > resident.paginator.totalCount) {
        if (parseInt(this.totalCount / perpage) + 1 === this.pageNo) {
          this.hasMore = false
          this.showLoad = false
          this.loadText = '暂无更多数据'
        }
      },
复制代码
复制代码

查询‘下一页’数据的方法:

复制代码
复制代码
      searchMore () {
        if (!this.hasMore) {
          return
        }
        this.pageNo++
        this.queryQuestionList()
      },
复制代码
复制代码

对列表进行前端分页:

复制代码
复制代码
     // 对问卷列表前端分页
      queryQuestionList () {
        if (!this.isFrontPage) {
          return
        }
        // this.questionList = _.cloneDeep(this.wholeList).splice(0, perpage * (this.pageNo + 1))
        this.questionList = this.questionList.concat(_.cloneDeep( this.wholeList ).splice(this.questionList.length, perpage * (this.pageNo + 1)))
        if (Number(this.isMyAnswer) === 1) {
          this.arrangeAnswer()
        }
        this._checkMore()
      },
复制代码
复制代码

注: this.questionList  表示,页面上展示的列表

     this.wholeList 表示,查询出来的所有列表

 

posted @   创业男生  阅读(562)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示