微信小程序~上拉加载onReachBottom

本文介绍在微信小程序中实现上拉加载更多的两种方法。第一种利用onReachBottom事件结合加载动画提升用户体验;第二种使用scroll-view组件的bindscrolltolower事件实现。文中详细解释了实现步骤及注意事项。

代码:

  //页面上拉触底事件的处理函数
  onReachBottom(e) {
    console.log("底部")// 滚动到页面执行 该 方法 
    wx.showToast({
      title: '加载中...',
      icon: 'loading',
      duration: 2000
    })
    /*
      这里执行你需要的请求数据追加到循环数组就好了
    */
  },
  onPageScroll(e) {
    console.log(e) //滚动条 滚动的位置(e.scrollTop)从头部开始计算
  },

 

原理:

上拉加载更多这个需求我相信应该应用颇为广泛的,今天说我认为两种可行的方式哈 。

一、第一个应该是最简单的一种实现方式,文档自带的一个api 可以监听滚动到页面底部的方法(onReachBottom) 、"onPageScroll"方法可以监听页面滚动条的位置。(PS:页面.json 中'onReachBottomDistance:number'默认为50,这个可以设置在距离底部多少px执行onReachBottom方法,具体使用看你需求。)
1.首先准备几个盒子 使其 超出page 页面高度产生滚动条、然后准备一个加载动画具体实现如下:

//wxml: arr是length为4的数组随意定义 只是为了撑高度的
<view class='warp'>
  <view wx:for="{{arr}}" class='bg_cl'></view>
</view> 
<!--加载动画  -->
<view class='bottom'> 
  <view class="loading">
        <text></text>
        <text></text>
        <text></text>
        <text></text>
        <text></text>
  </view>
</view>
// wxss ========================================
.warp{
  display: flex;
  flex-flow: column;}
.bg_cl{
  width: 100%;
  flex: 1;
  height: 400rpx;
  background: pink;}
.bg_cl:nth-child(2),.bg_cl:nth-child(4){
  background: purple;}
.bottom{
  line-height: 50rpx;
  font-size: 24rpx;
  display: flex; align-items: center;
  justify-content: center;}

/*过渡动画  */
.loading{
    width: 148rpx;
    height: 44rpx;}
.loading text{
    display: inline-block;
    width: 20rpx;
    height:20rpx;
    margin-right: 5px;
    border-radius: 50%;
    background:#999;
    -webkit-animation: load 1.04s ease infinite;
}
.loading text:last-child{
    margin-right: 0px; 
}
@-webkit-keyframes load{
    0%{
        opacity: 1;
        -webkit-transform: scale(1);
    }
    100%{
        opacity: 0.2;
        -webkit-transform: scale(.3);
    }
}
.loading text:nth-child(1){
    -webkit-animation-delay:0.13s;
}
.loading text:nth-child(2){
    -webkit-animation-delay:0.26s;
}
.loading text:nth-child(3){
    -webkit-animation-delay:0.39s;
}
.loading text:nth-child(4){
    -webkit-animation-delay:0.52s;
}
.loading text:nth-child(5){
    -webkit-animation-delay:0.65s;
}

//js=========================================================
onReachBottom(e){
    console.log("底部")// 滚动到页面执行 该 方法 
    wx.showToast({
      title: '加载中...',
      icon:'loading',
      duration:2000
    })
    /*
      这里执行你需要的请求数据追加到循环数组就好了
    */
  },
  onPageScroll(e){
    //console.log(e) //滚动条 滚动的位置(e.scrollTop)从头部开始计算
  },

 

 

 
 

 

第二种:可以用 scroll-view 组件,scroll-y为true 时允许纵向滚动、使用scroll-view 组件时需要设置固定的高度。组件中有一个bindscrolltolower 触底 /右边 方法。详情见官方文档(PS: 使用组件会在页面产生一个滚动条,而page中也会有一个此时会出现问题,可以在页面 .json配置文件中 设置:"disableScroll":true 页面整体不能上下滚动 等价于 wxss 中page{overflow:hidden} ;)

<!-- scroll-view  -->
 <scroll-view  scroll-y='true' style="height:{{height}}px" bindscroll='scrollt' bindscrolltolower='scrollBottom'>
  <view class='warp'>
    <view wx:for="{{arr}}" class='bg_cl'></view>
  </view>
</scroll-view> 
==============wxss 延用上方就好了 下方是js'=================
onLoad: function () {
    var that=this
    wx.getSystemInfo({
      success:res=>{
        console.log(res)
        this.setData({
          height: res.windowHeight //获取屏幕高度 赋值给scroll-view
        })
      }
    })
  },
  //scroll- view 滚动条 距顶部多少px
  scrollt(e){
    console.log(e.detail)
  },
  // scroll-view 滚动到底部触发 
  scrollBottom(e){
    console.log(" 我是scroll 的底部")
  //此处添加你的 请求方法就好了 这里不多做赘述了。
  }

 

 

 

 

 

 

.

转载于:https://www.cnblogs.com/jianxian/p/11122929.html

微信小程序中的onReachBottom事件可以用来实现上拉加载功能,当页面滚动到底部时,该事件会被触发。你可以在该事件中发送请求,获取更多的数据并渲染到页面上。 具体实现方法如下: 1. 在页面json文件中,将onReachBottom事件声明为页面的事件之一: ``` { "usingComponents": {}, "enablePullDownRefresh": true, "navigationBarTitleText": "示例页面", "onReachBottom": "onReachBottom" } ``` 2. 在页面的js文件中,编写onReachBottom事件的处理函数,该函数会在页面滚动到底部时被触发: ``` Page({ data: { dataList: [], // 数据列表 pageNum: 1, // 当前页数 pageSize: 10, // 每页显示的数据条数 hasMoreData: true // 是否还有更多数据 }, // 上拉加载更多 onReachBottom: function () { if (this.data.hasMoreData) { this.loadData(this.data.pageNum + 1) } }, // 加载数据 loadData: function (pageNum) { wx.showLoading({ title: '加载中' }) wx.request({ url: 'https://xxx.com/api/data', // 请求接口地址 data: { pageNum: pageNum, pageSize: this.data.pageSize }, success: res => { wx.hideLoading() if (res.data.code === 0) { let dataList = this.data.dataList.concat(res.data.data) this.setData({ dataList: dataList, pageNum: pageNum, hasMoreData: dataList.length < res.data.totalCount ? true : false }) } else { wx.showToast({ title: '加载失败', icon: 'none' }) } }, fail: err => { wx.hideLoading() wx.showToast({ title: '加载失败', icon: 'none' }) } }) } }) ``` 在上述代码中,loadData函数用于获取数据并渲染到页面,同时更新页码和是否还有更多数据的状态。onReachBottom事件中调用loadData函数,传入当前页码加1的值,以获取下一页的数据。 另外,如果后端返回的数据中包含总数据量,我们可以通过比较当前列表的数据数量和总数据量来判断是否还有更多数据,从而控制是否继续加载
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值