首先下载插件better-scroll,命令:npm i better-scroll --save
引入:import BScroll from "better-scroll";
代码如下,效果图见文首:
<template>
<div>
<div class="box"></div>
<!-- ........... -->
<div class="wrapper left" ref="wrapper">
<div class="bscroll-container">
<!-- 刷新提示信息 -->
<div class="top-tip">
<span class="refresh-hook">{{pulldownMsg}}</span>
</div>
<!-- 内容列表..........-->
<ul class="content">
<li v-for="item in data" :key="item" id="aaa">{{item}}</li>
</ul>
<!-- 底部提示信息........... -->
<div class="bottom-tip">
<span class="loading-hook">{{pullupMsg}}</span>
</div>
</div>
</div>
<!-- ............ -->
</div>
</template>
<script>
import BScroll from "better-scroll";
// let count = 1;
export default {
name: "Z",
data() {
return {
data: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
pulldownMsg: "下拉刷新 ",
pullupMsg: "加载更多 "
};
},
mounted() {
const that = this;
// 创建Better-Scroll对象
this.scroll = new BScroll(this.$refs.wrapper, {
probeType: 1, //滚动的时候会派发scroll事件,会节流
click: true //派发click事件
});
// //滑动结束松开事件
this.scroll.on("touchEnd", pos => {
//上拉刷新
if (pos.y > 200) {
setTimeout(() => {
this.pulldownMsg = " ";
this.scroll.refresh(); //重新计算高度
}, 2000);
} else if (pos.y < this.scroll.maxScrollY - 200) {
this.pullupMsg = " ";
}
});
}
};
</script>
<style scoped>
.box {
100%;
height: 40px;
background: #000;
}
.wrapper {
20%;
height: 500px;
background: rgb(245, 247, 249);
overflow: hidden;
position: relative;
}
#aaa {
100%;
height: 50px;
line-height: 50px;
text-align: center;
}
/* 下拉、上拉提示信息 */
.top-tip {
position: absolute;
top: -40px;
left: 0;
z-index: 1;
100%;
height: 40px;
line-height: 40px;
text-align: center;
color: #555;
}
.bottom-tip {
100%;
height: 35px;
line-height: 35px;
text-align: center;
color: #777;
position: absolute;
bottom: -35px;
left: 0;
}
</style>