在做項目的時候,有使用到better-scroll來完成滾動,在這里記錄一下對better-scroll的基本使用和一些使用心得。
安裝
npm install better-scroll --save
引用
import BScroll from 'better-scroll'
使用
let scroll = new BScroll(DOM[,option1,...],...)
option有很多,可以在官方文檔查看
https://ustbhuangyi.github.io/better-scroll/doc/zh-hans/options-advanced.html#scrollbar
瀏覽器滾動原理
瀏覽器的滾動條大家都會遇到,當(dāng)頁面內(nèi)容的高度超過視口高度的時候,會出現(xiàn)縱向滾動條;當(dāng)頁面內(nèi)容的寬度超過視口寬度的時候,會出現(xiàn)橫向滾動條。也就是當(dāng)我們的視口展示不下內(nèi)容的時候,會通過滾動條的方式讓用戶滾動屏幕看到剩余的內(nèi)容。
那么對于 better-scroll 也是一樣的道理,我們先來看一下 better-scroll 常見的 html 結(jié)構(gòu):(wrapper一定只能有一個子元素content,其他元素都要放進(jìn)content里面)
<div class="wrapper">
<ul class="content">
<li>...</li>
<li>...</li>
...
</ul>
</div>
為了更加直觀,我們再來看一張圖:(圖中wrpper應(yīng)該是wrapper)

image
綠色部分為 wrapper,也就是父容器,它會有固定的高度。黃色部分為 content,它是父容器的第一個子元素,它的高度會隨著內(nèi)容的大小而撐高。那么,當(dāng) content 的高度不超過父容器的高度,是不能滾動的,而它一旦超過了父容器的高度,我們就可以滾動內(nèi)容區(qū)了,這就是 better-scroll 的滾動原理。
在Vue中封裝一個獨(dú)立組件,用于作為滾動組件:Scroll
Home.vue(一部分)
<template>
<div id="home">
<scroll class="content"
ref="scroll"
:pull-up-load="true"
v-on:pullingUp="getMove"
:probe-type="3"
v-on:scroll="getPosition"
>
<!--tis:top-back標(biāo)簽不能放到scroll標(biāo)簽內(nèi),不然的話等下就算你設(shè)置了position:fixed它也不會固定-->
<top-back v-on:click.native="backClick" v-show="isShowBackTop"></top-back>
<script>
import Scroll from "../../components/common/scroll/Scroll";
import TopBack from "../../components/content/topback/TopBack";
export default {
name: "Home",
components: {
Scroll,
},
data(){
//默認(rèn)隱藏返回頂部圖標(biāo)
isShowBackTop: false,
},
created() {
},
mounted(){
},
methods: {
//上拉加載更多
getMove(){
this.getHomeGoods(this.currentType)
//(上拉加載默認(rèn)只能執(zhí)行一次,我們需要手動清除)
this.$refs.scroll.finishPullUp()
},
//判斷滾動的位置
getPosition(position){
if (-position.y > this.tabControlOffsetTop) {
this.isShowBackTop = true
}else {
this.isShowBackTop = false
}
},
}
}
</script>
<style scoped>
#home{
/*padding-top: 44px;*/
position: relative;
height: 100vh;
.content{
/*height: calc(100% - 93px);*/
position: absolute;
top: 44px;
bottom: 49px;
left: 0;
right: 0;
overflow: hidden;
}
</style>
</template>
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",
data(){
return {
scroll : null,
}
},
props: {
pullUpLoad: {
type: Boolean,
default: false
},
probeType: {
type: Number,
default: 0
}
},
mounted() {
//1.創(chuàng)建BetterScroll對象,并傳入DOM和選項(probetype,click,pullUpload)
this.scroll = new BScroll(this.$refs.wrapper,{
pullUpLoad: this.pullUpLoad,
click: true,
probeType: this.probeType
})
//監(jiān)聽scroll(滾動)事件,該事件會返回一個position
if (this.probeType ===2 || this.probeType === 3){
this.scroll.on('scroll',(position) => {
//發(fā)射自定義scroll事件給父組件,并發(fā)出position參數(shù)
this.$emit('scroll',position)
})
}
//監(jiān)聽pullingUp事件,監(jiān)聽到該事件完成上拉加載更多
//
if (this.pullUpLoad){
this.scroll.on('pullingUp',() => {
//發(fā)射自定義pullingUp事件給父組件
this.$emit('pullingUp')
})
}
},
methods: {
//封裝滾動的方法
topBack(x,y,time=300){
this.scroll.scrollTo(x,y,time)
},
//封裝完成刷新的方法
finishPullUp(){
this.scroll.finishPullUp()
},
////封裝刷新的方法
refresh(){
this.scroll.refresh()
}
}
}
</script>
<style scoped>
</style>
返回頂部
除了上面的基本滾動之外,我還加了一個小功能:返回頂部。
BackTop.vue
<template>
<div class="top-back">
<img src="../../../assets/img/tab-bar-img/1.jpg" alt="">
</div>
</template>
<script>
export default {
name: "TopBack",
}
</script>
<style scoped>
.top-back{
position: fixed;
right: 10px;
bottom: 55px;
}
.top-back img{
width: 40px;
height: 40px;
border-radius: 50% 50%;
}
</style>