小程序 上拉加载onReachBottom 的使用(以下是在mpvue中的例子)






  • <li class="clearfix" v-for="(item,index) in newsData" :key="index" @click="goToDet(item.id)">

<img mode="widthFix" :src="imgurl+item.theme_pic" alt="" />


{{item.title_name}}


查看更多》







<script>
  import { get, post } from "../../utils";
  import $store from "../../store/index";
export default {
  components: {},

  data () {
    return {
      imgurl:$store.state.str,
      newsData:[],
      pageNum: 1,
    }
  },
  methods:{
    async getNews(){
      const data = await get("Index/culture",{
          p:this.pageNum
      });
    
      if(data.code == 200){
        // this.newsData = data.data;
          this.newsData = this.newsData.concat(data.data);
      }else if(data.code == 400){
          wx.showToast({
              title: data.errMsg,
              icon: 'none',
              duration: 2000
          })
          return false;
      }
    },
    goToDet(id) {
      wx.navigateTo({
        url: '/pages/newsDetails/main?id='+id
      })
    },
  },
    // 上拉加载
    onReachBottom: function () {
        this.pageNum++
        this.getNews();
    },
  mounted() {

    this.getNews();

  },
  created () {

}
}
</script>

<style scoped>
.news{
  width: 100%;
}
.n_top{
  width: 100%;
}
.thtz{
  width: 100%;
}
.thtz ul{
  width: 100%;
  box-sizing: border-box;
}
.thtz li{
  margin: 3px 0;
  padding: 10px 10px;
  box-sizing: border-box;
  width: 100%;
  background: #ffffff;
  border-bottom:1px solid #ccc ;
}
.thtz li:last-child{
  border: 0;
}
.thtz .h_hhjd_zt{
  float: left;
  width: 35%;
}
.thtz .h_hhjd_yw{
  padding-left: 5%;
  box-sizing: border-box;
  position: relative;
  float: left;
  width: 62%;
}
.thtz .h_hhjd_zt img{
  width: 100%;
  height: 50px;
  border-radius: 5%;
}
.thtz .h_hhjd_yw h3{
  margin-top: 10px;
  font-weight: normal;
  font-size: 14px;
  color: #2B2828;
  line-height: 22px;
  overflow:hidden;
  text-overflow:ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  -webkit-box-orient: vertical;


}
.thtz .h_hhjd_yw>p{
  margin: 20px 0 10px;
}
.thtz .h_hhjd_yw>p>span{
  display: inline-block;
  margin-right: 10px;
  font-size: 16px;
  color: #f9b454;
}
</style>




注意事项:
    在样式中使用 position: fixed;  onReachBottom 无反应

转载于:https://www.cnblogs.com/AbbyXie/p/11571273.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
微信小程序的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、付费专栏及课程。

余额充值