当前位置:网站首页>上拉加载原理
上拉加载原理
2022-07-05 10:21:00 【码小龙.】
上拉加载原理
1、实现思路
滚定区域是给固定高度, 设置overflow-y:auto来实现
触发条件:可视高度 + 滚动高度 >= 实际高度
可视高度:通过dom的offsetHeight获取, 表示区域固定的高度; 但是更加推荐使用getBoundingClientRect()来获取高度, 因为使用前者会引起浏览器回流, 造成一些性能问题
滚动高度:滚动事件中通过e.target.scrollTop获取, 表示滚动条距离顶部的px
实际高度:通过dom的scrollHeight获取, 表示区域内所有内容的高度(包括滚定距离), 也就是实际的高度
2、基础实现
onScroll(e) {
let scrollTop = e.target.scrollTop
let scrollHeight = e.target.scrollHeight
let offsetHeight = Math.ceil(e.target.getBoundingClientRect().height)
let currentHeight = scrollTop + offsetHeight
if(currentHeight >= scrollHeight) {
console.log(‘触底’)
}
}
3、优化:添加触底距离
希望离底部还有一定距离就触发事件, 而不是等到完全触底, 和小程序的onReachBottom差不多
声明一个离底部的距离变量reachBottomDistance
此时的触发条件为:可视高度 + 滚动距离 + reachBottomDistance >= 实际高度
export default {
data() {
return {
reachBottomDistance: 100
}
},
methods: {
onScroll(e) {
let scrollTop = e.target.scrollTop
let scrollHeight = e.target.scrollHeight
let offsetHeight = Math.ceil(e.target.getBoundingClientRect().height)
let currentHeight = scrollTop + offsetHeight + this.reachBottomDistance
if(currentHeight >= scrollHeight) {
console.log(‘触底’)
}
}
}
}
4、优化:进入后只触发一次
在距离底部100px时成功触发事件, 但是由于100px以下的区域是符合条件的, 会导致一直触发, 所以需要做一些处理, 让其进入后只触发一次
export default {
data() {
return {
isReachBottom: false,
reachBottomDistance: 100
}
},
methods: {
onScroll(e) {
let scrollTop = e.target.scrollTop
let scrollHeight = e.target.scrollHeight
let offsetHeight = Math.ceil(e.target.getBoundingClientRect().height)
let currentHeight = scrollTop + offsetHeight + this.reachBottomDistance
if(currentHeight < scrollHeight && this.isReachBottom){
this.isReachBottom = false
}
if(this.isReachBottom) return
if(currentHeight >= scrollHeight) {
this.isReachBottom = true
console.log(‘触底’)
}
}
}
}
5、优化:实时获取可变, 不可变缓存
实时去获取位置信息会损耗性能, 应该将不变的缓存起来, 只获取实时可变的部分
export default {
data() {
return {
isReachBottom: false,
reachBottomDistance: 100,
scrollHeight: 0,
offsetHeight: 0
}
},
mounted() {
// -> 页面加载完成后将高度缓存起来
let dom = document.querySelector(‘.list’)
this.scrollHeight = dom.scrollHeight
this.offsetHeight = Math.ceil(dom.getBoundingClientRect().height)
},
methods: {
onScroll(e) {
let scrollTop = e.target.scrollTop
let currentHeight = scrollTop + this.offsetHeight + this.reachBottomDistance
if(currentHeight < this.scrollHeight && this.isReachBottom){
this.isReachBottom = false
}
if(this.isReachBottom) return
if(currentHeight >= this.scrollHeight) {
this.isReachBottom = true
console.log(‘触底’)
}
}
}
}
边栏推荐
- Constrained layout flow
- Events and bubbles in the applet of "wechat applet - Basics"
- 函数///
- 数组、、、
- [vite] 1371 - develop vite plug-ins by hand
- Today in history: the first e-book came out; The inventor of magnetic stripe card was born; The pioneer of handheld computer was born
- Should the dependency given by the official website be Flink SQL connector MySQL CDC, with dependency added
- 2022鹏城杯web
- Completion report of communication software development and Application
- 各位大佬,我测试起了3条线程同时往3个mysql表中写入,每条线程分别写入100000条数据,用了f
猜你喜欢

AtCoder Beginner Contest 258「ABCDEFG」

2022年T电梯修理操作证考试题及答案

ByteDance Interviewer: how to calculate the memory size occupied by a picture

What is the origin of the domain knowledge network that drives the new idea of manufacturing industry upgrading?

WorkManager的学习二
![[论文阅读] KGAT: Knowledge Graph Attention Network for Recommendation](/img/fa/d2061bc7bd437f062d46a009cf32cf.png)
[论文阅读] KGAT: Knowledge Graph Attention Network for Recommendation

Comparative learning in the period of "arms race"

How to judge that the thread pool has completed all tasks?

How can non-technical departments participate in Devops?

到底谁才是“良心”国产品牌?
随机推荐
Idea create a new sprintboot project
How to judge that the thread pool has completed all tasks?
Error: module not found: error: can't resolve 'xxx' in 'XXXX‘
Workmanager learning 1
How did automated specification inspection software develop?
flex4 和 flex3 combox 下拉框长度的解决办法
Window下线程与线程同步总结
php解决redis的缓存雪崩,缓存穿透,缓存击穿的问题
Golang应用专题 - channel
uniapp
How do programmers live as they like?
Solution to the length of flex4 and Flex3 combox drop-down box
Go项目实战—参数绑定,类型转换
【tcp】服务器上tcp连接状态json形式输出
C function returns multiple value methods
变量///
【观察】跨境电商“独立站”模式崛起,如何抓住下一个红利爆发时代?
【Vite】1371- 手把手开发 Vite 插件
IDEA新建sprintboot项目
手机厂商“互卷”之年:“机海战术”失灵,“慢节奏”打法崛起