• 小程序:下拉刷新+上拉加载+自定义导航栏


    下拉刷新 :

     +

     <scroll-view
                    scroll-y="true"                              允许纵向滚动

                    style="height: calc(100vh - 200rpx)"  下拉滚动的区域高度
                    refresher-enabled="true"             开启自定义下拉刷新  默认为false
                    :refresher-triggered="triggered"   设置当前下拉刷新状态,true 表示下拉刷新已经被触发,false 表示下拉刷新未被触发
                    :refresher-threshold="150"           设置自定义下拉刷新阈值
                    refresher-background="#eee"      下拉刷新的背景颜色
                    @refresherrefresh="onRefresh"   下拉刷新触发
                    @refresherrestore="onRestore"   上拉达到一定的距离
                    @refresherabort="onAbort"       上拉过程中取消操作
                >

            下拉刷新的内容区域

    data() {
            return {
                triggered: false, //下拉刷新标记
            };
        },

    methods: {

            // //下拉刷新
            onRefresh() {
                this.triggered = true
                this.getCount() //提醒消息数量
                this.getRemList() //提醒三条列表

                setTimeout(() => {
                    this.triggered = false;
                }, 1000);
            },
            // 在刷新过程中取消刷新
            onRestore() {
                this.triggered = 'restore'; // 需要重置
                console.log('onRestore');
            },
            // 在刷新过程中中止刷新
            onAbort() {
                console.log('onAbort');
            },

    }

    上拉加载:

                    scroll-y="true"
                    class="scroll-Y"
                    :style="{
                        height: `calc(100vh - 260rpx)`,
                    }"
                    @scrolltolower="lower"   //监听滚动事件,当页面滚动到底部时,绑定的方法会被触发。
                    :refresher-threshold="150"
                    refresher-background="#eee"
                    @refresherrefresh="onRefresh"
                    @refresherrestore="onRestore"
                    @refresherabort="onAbort"
                >
                   
                        
                   

     

    在data中定义开始页  步长  防止触底请求多次标记

    data() {
            return {
                datalist: [],//消息列表
                pageNum: 1, //开始页
                pageSize: 10, //步长
                total: '', //list总数

                freshFlag: true, //防止触底请求多次标记
            };
        },

    methods: {

            

        //上拉加载触发
            lower(e) {
                // 防止多次请求
                if (!this.freshFlag) {
                    return;
                }

                this.freshFlag = false;
                let length = this.datalist.length;
                // 判断是否超出最大长度,超出不请求
                if (this.total > length) {
                    this.pullDownNew();
                } else {
                    uni.showToast({
                        title: '没有更多了',
                        duration: 2000,
                    });
                    this.freshFlag = true;
                    return;
                }
            },

            //上拉加载请求最新数据拼接
            pullDownNew() {
                uni.showLoading({
                    title: '加载中',
                });
                this.pageNum++;
                getRemList({
                    pageNum: this.pageNum, //开始页
                    pageSize: this.pageSize, //步长
                })
                    .then(res => {
                        this.datalist = [...this.datalist, ...res.data.rows];
                        uni.hideLoading();
                        this.freshFlag = true;
                    })
                    .catch(err => {
                        uni.hideLoading();
                    });
            },
     

    }

    再说一下这个自定义顶部导航的布局方法:

    在data中定义状态栏的高度和自定义导航栏的高度

            statusBarHeight: '', // 状态栏高度
            barHeight: '', // 自定义导航栏高度

    uni.getSystemInfoSync() 是一个Uni-app框架中的API,用于同步获取设备系统信息。) 

    wx.getMenuButtonBoundingClientRect()是微信小程序的一个API,用于获取菜单按钮的边界信息。

    具体来说,这个API可以用来获取菜单按钮的位置和尺寸信息,包括元素的toprightbottomleftwidthheight等属性。这些信息可以帮助我们更好地了解菜单按钮在界面上的位置和大小,从而更好地进行布局和样式控制。)

    wx.getMenuButtonBoundingClientRect()拿到的值表示了菜单按钮的上边缘距离视窗上边缘多少像素,左边缘距离视窗左边缘多少像素,下边缘距离视窗下边缘多少像素,右边缘距离视窗右边缘多少像素,宽度为多少像素,高度为多少像素。

             // 状态栏高度
            this.statusBarHeight = uni.getSystemInfoSync().statusBarHeight;
            // 胶囊数据
            const { top, height } = wx.getMenuButtonBoundingClientRect();
            // 自定义导航栏高度 = 胶囊高度 + 胶囊的padding*2, 如果获取不到设置为38
            this.barHeight = height ? height + (top - this.statusBarHeight) * 2 : 38;

    整体代码如下: 

    1. <template>
    2. <view class="remind">
    3. <view class="bgc">
    4. <view :style="{ height: `${statusBarHeight}px` }"></view>
    5. <view
    6. class="customHead"
    7. :style="{
    8. height: `${barHeight}px`,
    9. 'line-height': `${barHeight}px`,
    10. }"
    11. >
    12. <text class="toHome iconfont" @click="toHome">&#xe669;</text>
    13. <text class="title">提醒消息</text>
    14. </view>
    15. </view>
    16. <view class="scrollList">
    17. <scroll-view
    18. scroll-y="true"
    19. class="scroll-Y"
    20. :style="{
    21. height: `calc(100vh - 260rpx)`,
    22. }"
    23. @scrolltolower="lower"
    24. :refresher-threshold="150"
    25. refresher-background="#eee"
    26. @refresherrefresh="onRefresh"
    27. @refresherrestore="onRestore"
    28. @refresherabort="onAbort"
    29. >
    30. <view>
    31. <!-- 下拉加载的内容 -->
    32. </view>
    33. </scroll-view>
    34. </view>
    35. </view>
    36. </template>
    37. <script>
    38. export default {
    39. data() {
    40. return {
    41. statusBarHeight: '', // 状态栏高度
    42. barHeight: '', // 自定义导航栏高度
    43. avatar: '', //头像
    44. datalist: [],//消息列表
    45. pageNum: 1, //开始页
    46. pageSize: 10, //步长
    47. total: '', //list总数
    48. freshFlag: true, //防止触底请求多次标记
    49. };
    50. },
    51. onLoad() {
    52. this.init();
    53. // 状态栏高度
    54. this.statusBarHeight = uni.getSystemInfoSync().statusBarHeight;
    55. // 胶囊数据
    56. const { top, height } = wx.getMenuButtonBoundingClientRect();
    57. // 自定义导航栏高度 = 胶囊高度 + 胶囊的padding*2, 如果获取不到设置为38
    58. this.barHeight = height ? height + (top - this.statusBarHeight) * 2 : 38;
    59. this.avatar = uni.getStorageSync('avatar') //头像
    60. },
    61. methods: {
    62. // 初始化
    63. init() {
    64. uni.showLoading({});
    65. getRemList({
    66. pageNum: this.pageNum, //开始页
    67. pageSize: this.pageSize, //步长
    68. }).then(res => {
    69. console.log(res);
    70. this.datalist = res.data.rows; //消息列表
    71. this.total = res.data.alltotal;
    72. uni.hideLoading();
    73. });
    74. },
    75. // 提醒消息页面回退
    76. toHome(){
    77. uni.navigateBack()
    78. },
    79. //上拉加载触发
    80. lower(e) {
    81. // 防止多次请求
    82. if (!this.freshFlag) {
    83. return;
    84. }
    85. this.freshFlag = false;
    86. let length = this.datalist.length;
    87. // 判断是否超出最大长度,超出不请求
    88. if (this.total > length) {
    89. this.pullDownNew();
    90. } else {
    91. uni.showToast({
    92. title: '没有更多了',
    93. duration: 2000,
    94. });
    95. this.freshFlag = true;
    96. return;
    97. }
    98. },
    99. //上拉加载请求最新数据拼接
    100. pullDownNew() {
    101. uni.showLoading({
    102. title: '加载中',
    103. });
    104. this.pageNum++;
    105. getRemList({
    106. pageNum: this.pageNum, //开始页
    107. pageSize: this.pageSize, //步长
    108. })
    109. .then(res => {
    110. this.datalist = [...this.datalist, ...res.data.rows];
    111. uni.hideLoading();
    112. this.freshFlag = true;
    113. })
    114. .catch(err => {
    115. uni.hideLoading();
    116. });
    117. },
    118. },
    119. };
    120. </script>
    121. <style lang="scss">
    122. .bgc{
    123. height: 260rpx;
    124. background: linear-gradient(180deg, #ffb588 -17.42%, rgba(255, 220, 167, 0) 119.43%);
    125. }
    126. .customHead {
    127. padding-left: 10rpx;
    128. display: flex;
    129. align-items: center;
    130. position: relative;
    131. .title {
    132. position: absolute;
    133. left: 50%;
    134. transform: translateX(-50%);
    135. }
    136. }
    137. .remind{
    138. .scrollList {
    139. width: 100vw;
    140. margin-bottom: 20rpx;
    141. }
    142. }
    143. </style>

  • 相关阅读:
    【蓝桥杯省赛真题41】Scratch电脑开关机 蓝桥杯少儿编程scratch图形化编程 蓝桥杯省赛真题讲解
    生产管理中,如何做好生产进度控制?
    热门Java开发工具IDEA入门指南——从Eclipse迁移到IntelliJ IDEA(三)
    【Flutter】Flutter 使用 pull_to_refresh 实现下拉刷新和上拉加载
    计算机毕业设计Java校园教育服务平台(源码+系统+mysql数据库+Lw文档)
    13Spring Boot整合第三方Druid数据源(自定义整合Druid)
    ID生成器代码重构问题
    VR数字党建:红色文化展厅和爱国主义教育线上线下联动
    鲁棒的非负监督低秩鉴别嵌入算法
    vue生命周期
  • 原文地址:https://blog.csdn.net/ll123456789_/article/details/133706755