网上看的代码。
原文:https://blog.csdn.net/wzg0817/article/details/108013765
附上API地址:https://better-scroll.github.io/docs/zh-CN/guide/base-scroll-api.html#%E4%BA%8B%E4%BB%B6
<template>
<div ref="wrapper">
<slot></slot>
</div>
</template>
<script>
import BScroll from 'better-scroll'
export default{
name: 'ScrollDefault',
props: {
probeType: {
type: Number,
default: 1
},
click: {
type: Boolean,
default: true
},
data: {
type: Array,
default: null
},
listenScroll: {
type: Boolean,
default: false
},
pullup: {
type: Boolean,
default: false
},
beforeScroll: {
type: Boolean,
default: false
},
refreshDelay: {
type: Number,
default: 20
}
},
methods:{
_initScroll(){
if (!this.$refs.wrapper) {
return ;
}
this.scroll = new BScroll(this.$refs.wrapper, {
probeType: this.probeType,
click: this.click
})
if(this.listenScroll){
this.scroll.on('scroll', (pos)=>{
this.$emit("scroll", pos);
})
}
if (this.pullup) {
//滚动结束
this.scroll.on('scrollEnd', ()=>{
if (this.scroll.y <= (this.scroll.maxScrollY + 50)) {
this.$emit('scrollToEnd')
}
})
}
if (this.beforeScroll) {
//手指放在滚动区域上
this.scroll.on('beforeScrollStart', () => {
this.$emit('beforeScroll')
})
}
},
refresh() {
this.scroll && this.scroll.refresh()
},
},
mounted(){
setTimeout(() => {
this._initScroll()
}, 20)
},
watch:{
data(){
setTimeout(()=>{
this.refresh();
}, this.refreshDelay)
}
}
}
</script>
<style>
</style>