uniapp下拉刷新组件代码
时间: 2023-03-21 17:02:20 浏览: 88
以下是一个示例的uniapp下拉刷新组件代码:
```html
<template>
<view>
<view class="refresh-text">{{refreshText}}</view>
<scroll-view :style="{height:scrollViewHeight + 'px'}" scroll-y="true" @scrolltoupper="onScrollToUpper" @scroll="onScroll">
<view class="list">
<view class="list-item" v-for="(item, index) in list" :key="index">
{{item}}
</view>
</view>
</scroll-view>
</view>
</template>
<script>
export default {
data() {
return {
list: [], // 数据列表
page: 1, // 当前页码
isLoading: false, // 是否正在加载数据
refreshText: '下拉刷新', // 刷新文本
startScrollTop: 0, // 滚动条开始位置
scrollViewHeight: 0, // 滚动区域高度
};
},
methods: {
// 下拉刷新
onScrollToUpper() {
if (this.isLoading) {
return;
}
this.isLoading = true;
this.refreshText = '正在刷新...';
// 模拟加载数据
setTimeout(() => {
this.list = ['刷新数据1', '刷新数据2', '刷新数据3'];
this.page = 1;
this.isLoading = false;
this.refreshText = '下拉刷新';
}, 2000);
},
// 滚动事件
onScroll(e) {
if (this.isLoading) {
return;
}
const scrollTop = e.detail.scrollTop;
const scrollViewHeight = e.detail.scrollHeight;
const windowHeight = uni.getSystemInfoSync().windowHeight;
if (scrollTop < this.startScrollTop && scrollTop <= 0) {
// 向上滚动并且滚动到顶部,触发下拉刷新
this.onScrollToUpper();
} else if (scrollViewHeight - (scrollTop + windowHeight) <= 10) {
// 滚动到底部,触发加载更多
this.onLoadMore();
}
this.startScrollTop = scrollTop;
this.scrollViewHeight = scrollViewHeight;
},
// 加载更多
onLoadMore() {
if (this.isLoading) {
return;
}
this.isLoading = true;
// 模拟加载数据
setTimeout(() => {
this.list = [...this.list, `加载更多数据${this.page + 1}`, `加载更多数据${this.page + 2}`, `加载更多数据${this.page + 3}`];
this.page++;
this.isLoading = false;
}, 2000);
},
},
};
</script>
```
这个示例包含一个下拉刷新组件和一个滚动加载更多组件,可以在下拉到顶部时触发下拉刷新,滚动到底部时触发加载更多。