uniapp 触底加载

方式一

onReachBottomDistance

缺点:需要整个页面滑动,局部滑动触发不了

javascript 复制代码
{
// pages.json
// 路由下增加 onReachBottomDistance
"path": "detailed/detailed",
  "style": {
    "navigationBarTitleText": "收益明细",
    "onReachBottomDistance": 50 //距离底部多远时触发 单位px
  }
},
javascript 复制代码
// detailed.js
// 触底 与 onLoad,onShow同级
onLoad(options) {},
onShow() {},
onReachBottom() {
  if (this.page < this.totalPages) {
    this.page++
  } else {
    return uni.showToast({
      title: '没有更多数据',
      icon: 'none',
      duration: 2000
    });
  }
  
  uni.showLoading({
  	title: '加载中'
  });
  
  // 请求
  this.getList()
}

方式二

scroll-view

文档:https://uniapp.dcloud.net.cn/component/scroll-view.html

使用竖向滚动时,需要给一个固定高度,通过 css 设置 height;

使用横向滚动时,需要添加white-space: nowrap;样式。

缺点:隐藏不了滚动条

javascript 复制代码
// 给固定高度
.roll-list {
  width: 100%;
  height: 100%;
  // 隐藏不了滚动条
  &::-webkit-scrollbar {
    display:none
  }
}
javascript 复制代码
<scroll-view
  class="roll-list"
  @scrolltolower="scrolltolower" // 触底事件
  scroll-y="true" // 竖向滚动
  show-scrollbar="false" // 隐藏滚动条
  >
	<view>示例</view>
	<view>示例</view>
	<view>示例</view>
</scroll-view>
javascript 复制代码
data() {
	return{
		this.page: 1,
    	this.totalPages: ''
    	this.list: []
	}
},
methods: {
	getList() {
	 api.list({page: this.pages.page,}).then(res => {
	    this.page = res.page
	    this.totalPages = res.totalPages
	    // es6 合并数组
	    this.list = [...this.list,...res.items]
	    uni.hideLoading(); // 关闭加载动画
	  })
	},
	scrolltolower() {
	  if (this.page < this.totalPages) {
	    this.page++
	  } else {
	    return uni.showToast({
	      title: '没有更多数据',
	      icon: 'none',
	      duration: 2000
	    });
	  }
	  
	  uni.showLoading({
	  	title: '加载中'
	  });
	  
	  // 请求
	  this.getList()
	}
}
相关推荐
老前端的功夫7 小时前
Vue 3 性能深度解析:从架构革新到运行时的全面优化
javascript·vue.js·架构
Irene19918 小时前
CLI 与 Vite 创建项目对比(附:最优解 create-vue)
vue·vite·cli·项目创建
前端 贾公子8 小时前
vue移动端适配方案 === postcss-px-to-viewport
前端·javascript·html
GISer_Jing9 小时前
AI营销增长:4大核心能力+前端落地指南
前端·javascript·人工智能
m0_4711996310 小时前
【场景】前端怎么解决离线收银、数据同步异常等场景问题
前端·javascript
陈小于11 小时前
windows(x86-x64)下编译JCEF
windows
栀秋66611 小时前
“无重复字符的最长子串”:从O(n²)哈希优化到滑动窗口封神,再到DP降维打击!
前端·javascript·算法
xhxxx11 小时前
不用 Set,只用两个布尔值:如何用标志位将矩阵置零的空间复杂度压到 O(1)
javascript·算法·面试
有意义11 小时前
斐波那契数列:从递归到优化的完整指南
javascript·算法·面试