• 移动端上拉分页加载更多(h5,小程序)


    1.h5,使用原生方式监听页面滚动上拉分页加载更多

    1. <template>
    2. <div></div>
    3. </template>
    4. <script>
    5. export default {
    6. data() {
    7. return {
    8. loadflag: true,
    9. maxpages: 0, //最大页码
    10. currentpage: 0, //当前页
    11. listData: [],
    12. config: {
    13. page: 1,
    14. pageSize: 15,
    15. total: 0,
    16. pages: 0,
    17. },
    18. }
    19. },
    20. created() {
    21. this.getList()
    22. window.addEventListener('scroll', this.handleScroll, true)
    23. },
    24. methods: {
    25. handleScroll() {
    26. const _this = this
    27. let scrollTop =
    28. document.documentElement.scrollTop || document.body.scrollTop
    29. //变量windowHeight是可视区的高度
    30. let windowHeight =
    31. document.documentElement.clientHeight || document.body.clientHeight
    32. //变量scrollHeight是滚动条的总高度
    33. let scrollHeight =
    34. document.documentElement.scrollHeight || document.body.scrollHeight
    35. //滚动条到底部的条件
    36. let data = scrollTop + windowHeight
    37. if (
    38. data == scrollHeight ||
    39. (scrollHeight - data <= 60 && _this.loadflag)
    40. ) {
    41. let currentpage = _this.currentpage
    42. let maxpages = _this.maxpages
    43. if (maxpages > currentpage && _this.loadflag) {
    44. _this.loadflag = false
    45. _this.config.page++
    46. _this.getList()
    47. }
    48. }
    49. },
    50. async getList() {
    51. let params = {
    52. page: this.config.page,
    53. pageSize: this.config.pageSize,
    54. }
    55. const res = await apiGet(posterGetBrochureList, params)
    56. this.isShowList = true
    57. if (res.code == 200) {
    58. const { total, records, current, pages } = res.data
    59. this.config.page = current //当前页
    60. this.config.pages = pages //总共多少页
    61. this.config.total = total //总条数
    62. this.maxpages = pages
    63. this.currentpage = current
    64. if (current <= pages) {
    65. this.loadflag = true
    66. this.listData = [...this.listData, ...records]
    67. } else {
    68. this.listData = records
    69. }
    70. }
    71. },
    72. },
    73. destroyed() {
    74. //离开页面的时候移除监听滚动事件,提高性能
    75. window.removeEventListener('scroll', this.handleScroll, true)
    76. },
    77. }
    78. </script>
    79. <style lang="scss" scoped></style>

    2.使用组件vant(van-list)

    36bdfd33802349e895b4485b6fb12102.png

    1. <template>
    2. <div></div>
    3. </template>
    4. <script>
    5. export default {
    6. data() {
    7. return {
    8. listLoading: false, //分页加载
    9. finished: false, //分页是否加载完成
    10. listData: [],
    11. config: {
    12. page: 1,
    13. pageSize: 10,
    14. total: 0,
    15. },
    16. }
    17. },
    18. created() {
    19. this.getList()
    20. },
    21. methods: {
    22. //关键函数,监听下拉加载更多
    23. onLoad() {
    24. this.getList()
    25. },
    26. async getList() {
    27. const formData = {
    28. page: this.config.page,
    29. pageSize: this.config.pageSize,
    30. }
    31. const res = await apiGet(apigetqueryArchiveMatchedPageList, formData)
    32. if (res.code == 200) {
    33. const data = res.data
    34. this.listData = [...this.listData, ...data.records]
    35. this.listLoading = false
    36. //判断如果当前请求条数小于十条,就停止下拉加载
    37. if (data.records.length < 10) {
    38. this.finished = true
    39. }
    40. this.config.page++
    41. }
    42. },
    43. },
    44. }
    45. </script>
    46. <style lang="scss" scoped></style>

     

    3.uniapp使用scroll-view上拉加载更多

    39fcb7642c4146bdb85f4482e0942775.png

    1. <scroll-view
    2. class="law_main"
    3. scroll-y="true"
    4. @scrolltolower="lower"
    5. lower-threshold="50"
    6. refresher-enabled
    7. :refresher-triggered="triggered"
    8. @refresherrefresh="onRefresh"
    9. >
    10. <view v-for="(item, index) in dataList" :key="index" class="content">
    11. <view class="time">
    12. {{ item.createTime }}
    13. </view>
    14. <view class="score">
    15. {{ item.integral }}
    16. </view>
    17. </view>
    18. <!-- <view class="more_text" v-if="showMoreData">没有数据了...</view> -->
    19. </scroll-view>

     

    tips:使用scroll-view上拉加载更多时,需要给一个高度,否则下拉加载将不生效 

    362c05d99a1c479ab3aa48b6928f9fcf.png

     

    4.uniapp监听页面触底加载更多onReachBottom

    1. <script>
    2. import { articlePageListApi } from '@/service/newpage.js'
    3. import qs from 'qs'
    4. export default {
    5. data() {
    6. return {
    7. params: {
    8. page: 1,
    9. pageSize: 10,
    10. },
    11. status: 'loadmore',// loadmore/loading / nomore
    12. artlist: [],
    13. hasmore: true,
    14. }
    15. },
    16. onLoad() {
    17. this.params.page = 1
    18. this.artlist = []
    19. this.hasmore = true
    20. this.getMylist()
    21. },
    22. //关键代码下拉刷新加载更多
    23. onReachBottom() {
    24. this.status = 'loading'
    25. if (this.hasmore) {
    26. this.status = 'loading'
    27. this.params.page++
    28. this.getMylist()
    29. } else {
    30. let timmer = setTimeout(() => {
    31. this.status = 'nomore'
    32. }, 1000)
    33. }
    34. },
    35. methods: {
    36. getMylist() {
    37. articlePageListApi(
    38. qs.stringify({
    39. ...this.params,
    40. }),
    41. ).then((res) => {
    42. if (res.data) {
    43. const { list } = res.data
    44. if (this.artlist?.length < list.total) {
    45. this.hasmore = true
    46. this.status = 'loadmore'
    47. } else {
    48. this.hasmore = false
    49. this.status = 'nomore'
    50. }
    51. this.artlist = [...this.artlist, ...list.records]
    52. }
    53. })
    54. },
    55. },
    56. }
    57. </script>

     

     

  • 相关阅读:
    starrocks进行数据的删除
    优雅的实现EasyPoi动态导出列的两种方式
    【机器学习-黑马程序员】人工智能、机器学习概述
    用户自定义配置管理最佳实践
    redis数据库
    基于Springboot开发实现二手交易商城
    《文化相对论》:危机重重的世界,对话才能产生转机
    音视频开发之旅(67) - 变速不变调之sonic源码分析
    98. 验证二叉搜索树
    【力扣 - 爬楼梯】
  • 原文地址:https://blog.csdn.net/m0_61476911/article/details/140947267