uniapp底部刷新加载

滚动内容

<scroll-view class="product-list" :scroll-y="true" @scrolltolower="scrollBottom">
	<view class="product-item" v-for="(item,index) in goodlist" :key="item.id">
		…………
	</view>
	<view v-if="loadingMore" class="nomore">加载中...</view>
</scroll-view>

对应css

.product-list {
	width: 100%;
	padding: 10px;
	box-sizing: border-box;
	padding-bottom: 110rpx;
	height: 80vh;
	// height: 300px;
	overflow-y: scroll;
}
.nomore {
	width: 100%;
	text-align: center;
	margin-top: 10px;
	height: 100rpx;
}

监测滚动到底部的方法

// 滚动到底部
scrollBottom() {
	console.log("滚动到底部!");
	if (this.loadingMore) return;  //滚动到最底部了
	if (this.current <= Math.ceil(this.total / 10)) {
		this.searchscroll = true;
		this.search(this.searchName); // 加载更多数据
		this.loadingMore = true;
	}			
},

定义数据

loadingMore: false, // 是否正在加载更多数据
current: 1,
total: 0,
pageSize: 10,
### 实现下拉刷新和触底加载UniApp 开发环境中,为了实现在 H5 网页网站、支付宝/微信小程序以及 Android 和 iOS 应用中的下拉刷新与触底自动加载更多数据功能,可以采用如下方式[^1]。 对于页面配置而言,需确保 `pages.json` 文件中对应页面的设置允许下拉刷新操作。具体来说,应将目标页面的 `enablePullDownRefresh` 属性设为 `true` 以便激活此特性[^2]: ```json { "pages": [ { "path": "pages/list/testPulldownRefreshReachBottom", "style": { "navigationBarTitleText": "产品列表", "enablePullDownRefresh": true, "app-plus": { "bounce": "vertical" } } } ] } ``` 针对 JavaScript 部分,则可以通过监听特定事件来处理逻辑。例如,在 Vue 组件内定义好用于存储数据项数组变量之后,可通过 `onLoadMore()` 方法响应用户的上滑动作并触发新的 API 请求获取额外的数据条目;而 `onPullDownRefresh()` 则用来捕获用户执行的手势从而重新抓取最新资料更新当前视图: ```javascript export default { data() { return { pageIndex: 1, // 当前页码 hasMoreData: true, // 是否还有更多数据 listItems: [] // 数据列表 }; }, methods: { onLoadMore() { if (!this.hasMoreData) return; this.pageIndex++; setTimeout(() => { const newData = this.fetchNewPage(this.pageIndex); if (newData.length === 0) { this.hasMoreData = false; // 如果新取得的数据长度为零则表示已无更多信息可载入 } else { this.listItems.push(...newData); // 合并新增加的内容至现有列表末端 } // 停止加载动画 uni.stopPullDownRefresh(); }, 1000); }, fetchNewPage(pageNum){ // 这里模拟异步请求过程... let result = []; for(let i=0;i<10;i++){ result.push(`Item ${pageNum}-${i}`); } return result; }, onPullDownRefresh(){ console.log('pull down refresh'); this.pageIndex = 1; this.hasMoreData = true; // 清空原有数据 this.listItems.splice(0); // 获取第一页的新鲜数据 const freshData = this.fetchNewPage(this.pageIndex); this.listItems.push(...freshData); // 结束本次刷新状态 uni.stopPullDownRefresh(); } }, onReachBottom() { this.onLoadMore(); // 页面到达最底部时调用 onLoadMore 函数 }, onPullDownRefresh() { this.onPullDownRefresh(); // 用户手动拉动屏幕顶部刷新时调用 onPullDownRefresh 函数 } } ``` 上述代码片段展示了如何利用 UniApp 提供的相关生命周期钩子函数 (`onReachBottom`, `onPullDownRefresh`) 来绑定自定义的行为处理器,并通过这些处理器内部的操作实现了预期效果——即当下方空间被完全展示出来的时候会尝试去追加更多的记录进来;反之如果用户向上拖拽界面顶端的话就相当于发起了一次重置性质的动作使得整个集合恢复成最初的状态.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值