当前位置:网站首页>Pull up loading principle
Pull up loading principle
2022-07-05 10:43:00 【Code Bruce Lee】
Pull up loading principle
1、 Realize the idea
The rolling area is given a fixed height , Set up overflow-y:auto To achieve
The trigger condition : Visual height + Rolling height >= Actual height
Visual height : adopt dom Of offsetHeight obtain , Indicates the fixed height of the area ; But it is more recommended getBoundingClientRect() To get the height , Because using the former will cause browser reflow , Cause some performance problems
Rolling height : Scroll through e.target.scrollTop obtain , Indicates the distance from the scroll bar to the top px
Actual height : adopt dom Of scrollHeight obtain , Indicates the height of all contents in the area ( Including rolling distance ), That is, the actual height
2、 Basic implementation
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(‘ Touch bottom ’)
}
}
3、 Optimize : Add the distance to the bottom
Hope to trigger the event when there is still a certain distance from the bottom , Instead of waiting until the bottom is completely reached , And small programs onReachBottom almost
Declare a distance variable from the bottom reachBottomDistance
The trigger condition at this time is : Visual height + Rolling distance + reachBottomDistance >= Actual height
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(‘ Touch bottom ’)
}
}
}
}
4、 Optimize : Only trigger once after entering
At the bottom of the distance 100px The event is triggered successfully , But because of 100px The following areas are eligible , It will cause constant triggering , So we need to do something about it , Let it only trigger once after entering
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(‘ Touch bottom ’)
}
}
}
}
5、 Optimize : Real time access to variable , Immutable cache
Getting location information in real time will lose performance , You should cache the unchanged , Only get the real-time variable part
export default {
data() {
return {
isReachBottom: false,
reachBottomDistance: 100,
scrollHeight: 0,
offsetHeight: 0
}
},
mounted() {
// -> After the page is loaded, it will be highly cached
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(‘ Touch bottom ’)
}
}
}
}
边栏推荐
- 想请教一下,十大券商有哪些?在线开户是安全么?
- Go project practice - Gorm format time field
- Workmanager Learning one
- 重磅:国产IDE发布,由阿里研发,完全开源!
- 基于昇腾AI丨爱笔智能推出银行网点数字化解决方案,实现从总部到网点的信息数字化全覆盖
- Go项目实战—参数绑定,类型转换
- Customize the left sliding button in the line in the applet, which is similar to the QQ and Wx message interface
- Implementation of wechat applet bottom loading and pull-down refresh
- csdn软件测试入门的测试基本流程
- SAP ui5 objectpagelayout control usage sharing
猜你喜欢
Window下线程与线程同步总结
[dark horse morning post] Luo Yonghao responded to ridicule Oriental selection; Dong Qing's husband Mi Chunlei was executed for more than 700million; Geely officially acquired Meizu; Huawei releases M
基于昇腾AI丨以萨技术推出视频图像全目标结构化解决方案,达到业界领先水平
[vite] 1371 - develop vite plug-ins by hand
双向RNN与堆叠的双向RNN
DGL中异构图的一些理解以及异构图卷积HeteroGraphConv的用法
How can non-technical departments participate in Devops?
非技術部門,如何參與 DevOps?
【JS】提取字符串中的分数,汇总后算出平均分,并与每个分数比较,输出
Atcoder beginer contest 254 "e BFS" f st table maintenance differential array GCD "
随机推荐
沟通的艺术III:看人之间 之倾听
What is the most suitable book for programmers to engage in open source?
AD20 制作 Logo
Workmanager Learning one
Go project practice - parameter binding, type conversion
一个可以兼容各种数据库事务的使用范例
How do programmers live as they like?
正则表达式
LDAP overview
数据库中的范式:第一范式,第二范式,第三范式
Glide conclusion
SLAM 01.人类识别环境&路径的模型建立
跨页面通讯
2022年化工自动化控制仪表考试试题及在线模拟考试
中职组网络安全C模块全漏洞脚本讲解包含4个漏洞的脚本
5g NR system architecture
Implementation of wechat applet bottom loading and pull-down refresh
Have the bosses ever encountered such problems in the implementation of flinksql by Flink CDC mongdb?
websocket
字符串、、