1. 程式人生 > 實用技巧 >實現首頁的上拉載入

實現首頁的上拉載入

為了實現首頁上的商品拖動即可載入更多,那麼在better-scroll中即需要用到pullUpLoad,這個配置用於做上拉載入功能,當設定其為true或者是一個Object的時候,可以開啟上拉載入。可以配置(threhold)來決定開始載入的時機,當上拉載入資料載入完畢後,需要執行finishPullUp方法。finishPullUp的作用當上拉載入資料載入完畢後,需要呼叫此方法告訴better-scroll資料已載入。

首先,我先在scroll元件當中做出瞭如下操作:

props:['probeType',"pullUpLoad"]

那我們只需要在所需要的父元件中,去設定pullUpload的值即可。

  mounted(){
    //1.建立BScroll物件
this.scroll = new BScroll(this.$refs.wrapper,{
click:true,
  probeType:this.probeType,
 //監聽滾動到底部
  pullUpLoad:this.pullUpLoad
})
    // this.scroll.scrollTo(0,0)
    //2.監聽滾動的位置
    this.scroll.on("scroll",(position)=>{
      // console.log(position);
      this.$emit("scroll",position)
    })
//監聽滾動到底部 if(this.pullUpLoad){ this.scroll.on("pullingUp",()=>{ // console.log("監聽"); this.$emit("pullingUp") }) } }
 methods:{
    finishPullUp(){
      this.scroll.finishPullUp()
    }
  }

那麼在home.vue中我們可以做如下設定:

<scroll class="content" ref="scroll" :probe-type="3" @scroll="contentscroll" :pull-up-load="true" @pullingUp="loadmore">
methods:{
   loadmore(){
    this.gethomegoods(this.currenttype)
   },
gethomegoods(type){
const page = this.goods[type].page + 1
gethomegoods(type,page).then(res =>{
// console.log(res);
// res =>pop的第一頁
this.goods[type].list.push(...res.data.data.list)
this.goods[type].page+= 1
//完成上拉載入更多
this.$refs.scroll.finishPullUp()
})
}

這樣即可實現了首頁的上拉載入。