Use Setup
Install vue2-better-scroll
yarn add vue2-better-scroll
npm install vue2-better-scroll -s
Vue mount
import Vue from 'vue'
import VueBetterScroll from 'vue2-better-scroll'
var Vue = require('vue')
var VueBetterScroll = require('vue2-better-scroll')
Vue.use(VueBetterScroll)
import { VueBetterScroll } from 'vue2-better-scroll'
export default {
components: {
VueBetterScroll
}
}
<script src="./dist/vue-better-scroll.js"></script>
Use in SPA
<template>
<div id="app">
<header>vue-better-scroll demo</header>
<main class="position-box">
<vue-better-scroll class="wrapper"
ref="scroll"
:scrollbar="scrollbarObj"
:pullDownRefresh="pullDownRefreshObj"
:pullUpLoad="pullUpLoadObj"
:startY="parseInt(startY)"
@pulling-down="onPullingDown"
@pullin-up="onPullingUp">
<ul class="list-content">
<li class="list-item"
v-for="item in items">{{item}}</li>
</ul>
</vue-better-scroll>
</main>
<button class="go-top"
@click="scrollTo">返回顶部</button>
</div>
</template>
<script>
import VueBetterScroll from '../dist/vue-better-scroll'
let count = 1
export default {
name: 'app',
components: { VueBetterScroll },
data() {
return {
scrollbarObj: {
fade: true
},
pullDownRefreshObj: {
threshold: 90,
stop: 40
},
pullUpLoadObj: {
threshold: 0,
txt: {
more: '加载更多',
noMore: '没有更多数据了'
}
},
startY: 0,
scrollToX: 0,
scrollToY: 0,
scrollToTime: 700,
items: []
}
},
mounted() {
this.onPullingDown()
},
methods: {
scrollTo() {
this.$refs.scroll.scrollTo(this.scrollToX, this.scrollToY, this.scrollToTime)
},
getData() {
return new Promise(resolve => {
setTimeout(() => {
const arr = []
for (let i = 0; i < 10; i++) {
arr.push(count++)
}
resolve(arr)
}, 1000)
})
},
onPullingDown() {
console.log('下拉刷新')
count = 0
this.getData().then(res => {
this.items = res
this.$refs.scroll.forceUpdate(true)
})
},
onPullingUp() {
console.log('上拉加载')
this.getData().then(res => {
this.items = this.items.concat(res)
if (count < 30) {
this.$refs.scroll.forceUpdate(true)
} else {
this.$refs.scroll.forceUpdate(false)
}
})
}
}
}
</script>
<style>
.position-box {
position: fixed;
top: 40px;
left: 0;
right: 0;
bottom: 0;
}
</style>