uniapp pc端和移动端列表上拉刷新加载分页

效果图:
在这里插入图片描述
在这里插入图片描述

tips: 实现了pc端和移动端兼容

代码实现:

html:

<view v-if="answerList.length >= 1" style="padding:30rpx 0;color:#888;text-align: center;">
	<view>{{loadMore}}</view>
</view>

data:

data() {
	return {
		pageNo: 1,
		pageSize: 10,
		totalCount: 0,
		loadMore: '上拉加载更多'
	}
}
// uniapp移动端触底生命周期
onReachBottom() {
	this.getList()
},
mounted() {
	// 判断当前是移动端还是pc端
	if (this.isMobile()) {
		console.log('移动端')
	} else {
		console.log('pc端')
		window.addEventListener('scroll', this.scrollBottom)
	}
			
},
methods: {
	isMobile() {
		let flag = navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)
		return flag
	},
	// pc端触底方法
	scrollBottom() {
		// 变量scrollTop为当前页面的滚动条纵坐标位置
		 let scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
		// 变量windowHeight 是可视区的高度
		 let windowHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
	    // 变量scrollHeight 是滚动条的总高度
	    let scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight;
			
	    // 到底的条件
	    if (scrollTop + windowHeight >= scrollHeight - 100) {
	      // 到底后要触发的事件
	      console.log('pc端触底了');
	      this.getList()
	  }
	},
	getList() {
		if ((this.pageNo * this.pageSize) < this.totalCount) {
			this.loadMore = '正在加载中……'
			this.pageNo++
			this.keywordsSearch()
		} else {
			this.loadMore = '已无更多'
		}
	},
	// 调用后端接口拿到数据
	keywordsSearch() {
		const params = {
			keywords: this.search.searchValue,
			type: this.search.type,
			pageNo: this.pageNo,
			pageSize: this.pageSize
		}
		searchKeywords(params).then(res => {
			if (res.code == 200) {
				this.answerList = this.answerList.concat(res.result.records)
				this.totalCount = res.result.total
				if (this.pageNo == 1 && this.answerList.length < 10) {
					this.loadMore = '已无更多'
				}
			}
		})
	},
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱喝冰可乐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值