vue2+better-scroll实现列表上拉刷新下拉加载功能

dom结构

 <main ref="wrapper">
            <div class="order_list">
                <div class="down">
                    {{ pulldownMsg }}
                </div>
                <div class="up" v-if="orderList.length > 7">
                    <van-loading v-if="!hasMoreData" size="24px">加载中...</van-loading>
                    <p v-else>已经显示全部了</p>
                </div>
                <div class="order_title">
                    {{ orderListType === 'my' ? '我的列表' : '组内列表' }}
                </div>
                <div class="list" ref="orderList" v-if="orderList.length > 0">
                    <OrderItem v-for="item in orderList" :key="item.id" :order="item"></OrderItem>
                </div>
                <van-empty v-else description="暂无数据" />
            </div>
        </main>

 注意!!! 一定要给bsroll绑定的父级元素明确的高度

import BScroll from 'better-scroll';

初始化

放在mounted里

// 初始化better-scroll
        initBScroll() {
            this.$nextTick(() => {
                this.scroll = new BScroll(this.$refs.wrapper, {
                    scrollY: true,
                    click: true,
                    probeType: 3,
                });
                this.bindScrollEvents();
            });
        },

上拉刷新

        bindScrollEvents() {
            this.scroll.on('touchEnd', async (pos) => {
                if (pos.y > 40) {
                    await this.getData();
                    this.pulldownMsg = "下拉刷新";
                } else if (pos.y < (this.scroll.maxScrollY - 30)) {
                    await this.loadMoreOrders();
                }
            });
            this.scroll.on('scroll', (pos) => {
                if (pos.y > 40) {
                    this.pulldownMsg = "释放立即刷新";
                } else {
                    this.pulldownMsg = "下拉刷新";
                }
            });
        },

下拉加载
 

async loadMoreOrders() {
            if (!this.hasMoreData) {
                return
            }
            this.isLoading = true;

            this.pageNo++;
            const newOrders = await this.fetchData({ pageNo: this.pageNo });
            if (newOrders.length === 0) {
                this.pageNo--;
                this.hasMoreData = false;
                return
            }
            this.orderList = [...this.orderList, ...newOrders];
            this.isLoading = false;
            this.$nextTick(() => {
                this.scroll.refresh();
            });
        },

css
一定要给定被绑定的dom一个高度

main {
    flex: 1;
    overflow: hidden;
}

  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值