页面结构:
<div ref="scrollContent" class="scrollContent">
<div>内容</div>
</div>
样式:
<style>
.scrollContent {
width: 100%;
height: 100%;
overflow-y: scroll;
}
</style>
js代码:
data () {
isFinish: false // 实现下拉松手刷新的操作
},
mounted () {
// 实现下拉查看历史消息
const _this = this
const container = document.querySelector('.scrollContent');
const refreshThreshold = 80; // 下拉刷新阈值(持续下拉的阈值)
const triggerThreshold = 40; // 触顶刷新阈值(滚动到顶部的阈值)
let startY = 0;
let isDragging = false;
let isRefreshing = false;
container.addEventListener('touchstart', function(e) {
startY = e.touches[0].clientY;
isRefreshing = false;
});
container.addEventListener('touchmove', function(e) {
const currentY = e.touches[0].clientY;
const distance = currentY - startY;
if (distance > refreshThreshold && !isDragging && !isRefreshing) {
isDragging = true;
// 继续下拉一段距离后,执行下拉刷新操作
}
if (container.scrollTop === 0 && distance >= triggerThreshold && !isRefreshing) {
isRefreshing = true;
// 滚动到顶部且持续下拉一段距离后,执行下拉触顶刷新操作
_this.isFinish = true
}
if (distance <= 0) {
isDragging = false;
}
});
container.addEventListener('touchend', function() {
startY = 0;
isDragging = false;
// 下拉直到松手才允许进行获取历史记录的操作
if (_this.isFinish) {
//在这个地方写需要执行下拉触发的方法
}
_this.isFinish = false
});
}