better-scroll做下拉刷新,上拉加載實例

scroll.vue組件
<template>
  <div ref="wrapper" class="scroll-wrap">
    <div>
      <div v-if="pulldown" class="pulldown"
      :style="`margin-top:${dragTip.translate}px`">
        <div class="clear" v-if="dragTip.showLoding">
          <div class="fl"><img width="30" src="~common/js/loading9.svg"></div>
          <div class="fl lh30 ml10">{{dragTip.text}}</div>
        </div>
      </div>
      <slot></slot>
      <div v-if="pullup" class="pullup">
        <div class="clear"v-if="!isDone">
          <div class="fl"><img width="30" src="~common/js/loading10.svg"></div>
          <div class="fl lh30 ml10">加載中.....</div>
        </div>
        <div class="list-donetip" v-if="!isLoading && isDone">
            <slot name="doneTip">沒有數據了</slot>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import BScroll from 'better-scroll'
export default {
  props:{
    probeType:{
      type:Number,
      default:1
    },
    /**
     * 點擊列表是否派發click事件
     */
    click:{
      type:Boolean,
      default:false
    },
    /**
     * 是否開啓橫向滾動
     */
    scrollX:{
      type:Boolean,
      default:false
    },
    /**
     * 是否派發滾動事件
     */
    listenScroll:{
      type:Boolean,
      default:false
    },
    /**
     * 列表數據
     */
    data:{
      type:[Object,Array,String],
      default:null
    },
    /**
     * 是否派發滾動到底部的事件,用於上拉加載
     */
    pullup:{
      type:Boolean,
      default:false
    },
    /**
     * 是否派發頂部下拉的事件,用於下拉刷新
     */
    pulldown:{
      type:Boolean,
      default:false
    },
    /**
     * 是否派發列表滾動開始的事件
     */
    deforeScroll:{
      type:Boolean,
      default:false
    },
    /**
     * 當數據更新後,刷新scroll延時
     */
    refreshDelay:{
      type:Number,
      default:20
    }
  },
  data(){
    return{
      dragTip:{
        text:"下拉刷新",
        translate:-50,
        showLoding:false
      },
      isLoading: false,
      isDone: false,
    }
  },
  mounted(){
    setTimeout(() => {
      this._initScroll()
    },20)
  },
  methods:{
    _initScroll(){
      if(!this.$refs.wrapper){
        return
      }
      // better-scroll的初始化
      this.scroll = new BScroll(this.$refs.wrapper,{
        probeType:this.probeType,
        click:this.click,
        scrollX:this.scrollX
      })
      // 是否派發滾動事件
      if(this.listenScroll){
        let me = this
        this.scroll.on('scroll',(pos) => {
          if(this.listenScroll){
            me.$emit('scroll',pos)
          }
        })
      }
      // 是否派發滾動到底部事件,用於上拉加載
      if(this.pullup){
        this.scroll.on('scrollEnd',() => {
          if(this.scroll.y <= (this.scroll.maxScrollY + 50)){
            //所有數據加載完畢後
            this.$on('infinitescroll.loadedDone', () => {
                this.isLoading = false;
                this.isDone = true;
            });
            //單次請求數據加載完畢後
            this.$on('infinitescroll.finishLoad', (ret) => {
                this.isLoading = false;
            });
            //重新初始化
            this.$on('infinitescroll.reInit', () => {
                this.isLoading = false;
                this.isDone = false;
            });
            this.$emit('scrollToEnd')
          }
        })
      }
      // 是否派發頂部下拉事件,用於下拉刷新
      if(this.pulldown){
        this.scroll.on('scroll',(pos) => {
          //顯示下拉刷新loding
          this.dragTip.showLoding = true
          //隱藏底部加載loding
          this.isLoading = false
          if(pos.y > 50){
            this.dragTip.text = "釋放刷新"
          }
        })
        this.scroll.on('touchEnd',(pos) => {
          if(pos.y > 50){
            this.dragTip.translate = 0
            this.dragTip.text = "刷新中..."
            //重新初始化
            this.$on('pullrefresh.finishLoad', this.resetParams);
            this.$emit('pulldown',pos)
          }
        })
      }
      // 是否派發列表滾動開始的事件
      if(this.beforeScroll){
        this.scroll.on('beforeScrollStart',() => {
          this.$emit('beforeScroll')
        })
      }
    },
    resetParams(){
      setTimeout(() => {
        this.isLoading = false;
        this.isDone = false;
        this.dragTip = {
          text:"下拉刷新",
          translate:-50,
          showLoding:false
        }
      },600)
    },
    disable(){
      this.scroll && this.scroll.disable()  
    },
    enable(){
      this.scroll && this.scroll.enable()
    },
    refresh(){
      this.scroll && this.scroll.refresh()
    },
    scrollTo(){
      this.scroll && this.scroll.scrollTo.apply(this.scroll, arguments)
    },
    scrollToElement() {
      this.scroll && this.scroll.scrollToElement.apply(this.scroll, arguments) 
    }
  },
  watch:{
    data(){
      setTimeout(() => {
        this.refresh()
        new BScroll(this.$refs.wrapper,{
          probeType:this.probeType,
          click:this.click,
          scrollX:this.scrollX
        })
      },this.refreshDelay)
    }
  }
}
</script>

<style scoped lang="stylus" type="stylesheet/stylus">
.scroll-wrap
    height:100%
    overflow:hidden
/* 下拉刷新 */
.pulldown,.pullup
    width:100%
    height:50px
    position:relative
    div.clear
        padding:10px 0px
        font-size:.28rem
        position:absolute
        left:50%
        top:5px
        transform:translate(-50%,0)
.list-donetip
    text-align:center
    line-height:50px
    font-size:.28rem
</style>

home.vue 調用better-scroll組件

<template>
  <div class="ScrollBox">
    <scroll 
    ref="pullrefresh"
    :pullup="isShow"
    :pulldown="isShow"
    @pulldown="loadDemo"
    @scrollToEnd="loadup">
      <div v-for="item in list" class="lh40 pb30">
        <div class="clear">
          <div class="fl">{{item.title}}</div>
          <div class="fl">{{item.create_time}}</div>
        </div>
        <div>{{item.link}}</div>
      </div>
    </scroll>
  </div>
</template>

<script>
import Scroll from 'base/scroll/scroll'
import Slider from 'base/slider/slider'
import {notice} from 'common/js/getData'
export default {
  components:{Scroll,Slider},
  data () {
    return {
      isShow: true,
      list:[],
      page:1
    }
  },
  mounted(){
    this._getDataInfo(1)
  },
  methods:{
    loadDemo(){
      this.list = []
      //下拉刷新重新初始化
      this.$refs.pullrefresh.$emit('pullrefresh.finishLoad');
      this._getDataInfo(1)
    },
    loadup(){
      if(!this.page) return
      this.page ++ 
      //上拉加載重新初始化
      this.$refs.pullrefresh.$emit('infinitescroll.reInit');
      this._getDataInfo(this.page)
    },
    _getDataInfo(page){
      notice(page).then((res) => {
        if(res.list.length >= 10){
          this.list = [...this.list, ...res.list]
          //單次請求數據加載完畢後
          this.$refs.pullrefresh.$emit('infinitescroll.finishLoad');
        }else{
          //把page設置成false 數據已經加載完畢了 不用在加載了
          this.page = false
          //所有數據加載完畢後
          this.$refs.pullrefresh.$emit('infinitescroll.loadedDone');
        }
      })
    }
  }
}
</script>

<style scoped lang="stylus" type="stylesheet/stylus">
@import "~common/stylus/variable"
.ScrollBox
    width:100%
    position:fixed
    top:0px
    bottom:0px
</style>

樣式有些粗糙  主要是做scroll組件裏面的刷新和加載 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章