VUE better-scroll的使用场景加各种实际案例

本文详细介绍了如何使用 Better Scroll 插件封装一个可复用的 Vue.js 滚动组件,包括实现上拉加载更多、点击回到顶部、标题滚动同步等功能,以及如何在项目中正确使用该组件。

一.先在终端进行安装better-scroll

npm install better-scroll --save

二.我们封装一个scroll组件,各种地方只要引入就行了

Scroll.vue

      <template>
  <div class="wrapper" ref="wrapper">
    <div class="content">
      <slot></slot>
    </div>
  </div>
</template>

<script>
  import BScroll from "better-scroll"
export default {
  name:"Scroll",
  props:{
    probeType:{
      type:Number,
      default:0
    },
    pullUpLoad:{
      type:Boolean,
      default:false
    }
  },
  data(){
    return{
      scroll:null,
    }
  },
 mounted() {
      // 1.创建BScroll对象,$refs获取元素
      this.scroll = new BScroll(this.$refs.wrapper, {
        click: true,
        probeType: this.probeType,
        pullUpLoad: this.pullUpLoad
      })
      // console.log(this.$refs.wrapper)
      // 2.监听滚动的位置
      this.scroll.on('scroll', (position) => {
        this.$emit('scroll', position)
        
      })

      // 3.监听上拉事件
      this.scroll.on('pullingUp', () => {
        this.$emit('pullingUp')
      })
    }, 
  methods:{
    scrollTo(x,y,time=300){
      this.scroll && this.scroll.scrollTo(x,y,time)
    },
    finishPullUp(){
      //提示本次加载完成(pullUpLoad)
      this.scroll && this.scroll.finishPullUp()  
    },
    refresh(){
      this.scroll && this.scroll.refresh()
    }
  }


}
</script>

<style>
  .wrapper{
    position: relative;
  }
</style>

三.点击回到顶部按钮:当我滚动到一定位置的时候显示这个按钮,并且点击回回到顶部
首先肯定需要封装一个回到顶部的插件
BackTop.vue

<template>
  <div class="back-top">
    <img src="" alt="">
  </div>
</template>

<script>
export default {
  name:'BackTop'
}
</script>

<style scoped>
  .back-top {
    position: fixed;
    right: 8px;
    bottom: 55px;
    z-index: 99
  }

  .back-top img {
    width: 43px;
    height: 43px;
  }
</style>

使用better-scroll插件之后,我们原生的滚动就不起效果了,所以我们需要用Scroll把其他需要滚动的组件包裹起来

 import Scroll from '' 引入这个组件
<scroll ref="scroll" :probe-type="3" @scroll="backIsShow">
需要滚动的组件全部包裹起来
</scroll>

import BackTop from '' 引入我们封装的BackTop组件

<BackTop v-show="isShow" @click.native="backclick" />
data:{
 isShow:false,
}
methods:{
backclick(){
  this.$refs.scroll.scrollTo(0,0,1000) 这就实现了点击回到顶部
},
 backIsShow(position){
this.isShow = (-position.y)>1000 这就实现了滚动到一定位置就显示

},
}

四.上拉加载更多:当我滚动到一定区域的时候,请求数据,类似无限滚动

   import Scroll from '' 引入这个组件
<scroll ref="scroll" :probe-type="3" @scroll="backIsShow"  :pull-up-load="true"
@pullingUp="loadmore">
需要滚动的组件全部包裹起来
</scroll>

   methods:{
    loadmore(){
    封装网络请求函数
  },
   在我们封装的网络请求,上拉数据增加后面加上一行代码
   this.$refs.scroll.finishPullUp()
   }

五.点击标题滚动到对应内容,并且滚动内容显示对应标题

   import Scroll from '' 引入这个组件
<scroll ref="scroll" :probe-type="3" @scroll="scroll" 
>
需要滚动的组件全部包裹起来
</scroll>
<tabbar @click="barclick"/>标题组件

data:{
 currentIndex: 0,
 saveY:null,
},
methods:{
 barclick(index){ 标题的index
  this.$refs.scroll.scrollTo(0,-this.saveY[index],100) 实现了点击滚动
},
backclick(){
  this.$refs.scroll.scrollTo(0,0,500)
},
scroll(position){
  let p = -position.y;
  //返回顶部按钮的展示
  this.isShow = p>1000
  //代码不臃肿,bug注意,迭代中的i是字符串类型
    for(let i in this.saveY){
      if(this.$refs.nav.currentIndex!=i){
        if(p>=this.saveY[i] && p<this.saveY[i*1+1]){
          this.$refs.nav.currentIndex=i*1
        } 实现了滚动内容显示对应标题
      }
    }
  },

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值