better-scroll封装具有上拉加载、下拉刷新等组件(Vue)

使用方法:
	使用:
		将要滑动的内容放进组件标签内,作为插槽
		
	下拉刷新、上拉加载
		在该组件标签上自定义@loadRefresh='x',@loadMore='x'函数,作为下拉加载、上拉刷新回调函数
	
	回到顶部
		自定义临界值
	
	具体配置看代码注释

代码示例:

<template>
   <div class='scroll'>
     <div class='scroll-cont'>
        <slot x='jeff'></slot>
     </div>
     <div class='back' v-show='show' @click='backTop'></div>
   </div>
</template>

<script>
import BScroll from 'better-scroll'

export default {
  name:'betterScroll',
  data(){
    return{
      scroll:null,
      show:false
    }
  },
  mounted()
  {
     this.$nextTick(()=>{
        this.scroll=new BScroll('.scroll',{
            probeType:2,
            click:true,
            //下拉刷新配置
            pullDownRefresh: { 
                threshold: 50,
                stop: 0
            },
            //上拉加载配置
            pullUpLoad:{
                threshold: 50
            }

        });

        //监听滚动事件
        this.scroll.on('scroll',(e)=>{
            //回到顶部按钮出现临界值
            this.show=-e.y>400?true:false
        })

        //监听下拉刷新
        this.scroll.on('pullingDown',()=>{
            
            this.$emit('loadRefresh')
            this.scroll.finishPullDown();
        })
        //监听上拉加载
        this.scroll.on('pullingUp',()=>{
            
            this.$emit('loadMore')
            this.scroll.finishPullUp();

        })

     })

  },
  methods:{
      //回到顶部
      backTop()
      {
        this.scroll.scrollTo(0,0,300);
      }
  }

}
</script>

<style scoped>
.scroll{
    /* 修改滚动区域 */
    height: 100vh;
    overflow: hidden;
}
.scroll>.scroll-cont{
    padding-top:50px;
}
.back{
    position: fixed;
    bottom:90px;
    right: 20px;
    width: 50px;
    height: 50px;
    background-color: white;
    text-align: center;
    line-height: 50px;
    color:#FF8E96;
    border-radius: 50px;
    font-weight: bold;
    font-size:20px ;
    
}
</style>

自定义上拉加载、下拉刷新的节流防抖函数

    //上拉加载
    async _loadMore()
    {

      if(this.timer)
      {
        clearInterval(this.timer);
      }
      this.timer=setTimeout(() => {
      	网络请求操作
      
        console.log('加载触发了');  
      }, 1000);
      

    },
    //下拉刷新
    async _loadRefresh()
    {
      if(this.timer)
      {
        clearInterval(this.timer);
      }
      this.timer=setTimeout(()=>{
  		网络请求操作
  		
        console.log('刷新了');
      },1000);

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值