当前位置:网站首页>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 ’)
}
}
}
}
边栏推荐
- PHP solves the problems of cache avalanche, cache penetration and cache breakdown of redis
- Shortcut keys for vscode
- SAP ui5 objectpagelayout control usage sharing
- 变量///
- C语言活期储蓄账户管理系统
- AtCoder Beginner Contest 258「ABCDEFG」
- [vite] 1371 - develop vite plug-ins by hand
- Web3基金会「Grant计划」赋能开发者,盘点四大成功项目
- Based on shengteng AI Aibi intelligence, we launched a digital solution for bank outlets to achieve full digital coverage of information from headquarters to outlets
- 正则表达式
猜你喜欢
Crawler (9) - scrape framework (1) | scrape asynchronous web crawler framework
基于昇腾AI丨以萨技术推出视频图像全目标结构化解决方案,达到业界领先水平
How does redis implement multiple zones?
2022鹏城杯web
微信核酸检测预约小程序系统毕业设计毕设(6)开题答辩PPT
[observation] with the rise of the "independent station" model of cross-border e-commerce, how to seize the next dividend explosion era?
Who is the "conscience" domestic brand?
2022年化工自动化控制仪表考试试题及在线模拟考试
【黑马早报】罗永浩回应调侃东方甄选;董卿丈夫密春雷被执行超7亿;吉利正式收购魅族;华为发布问界M7;豆瓣为周杰伦专辑提前开分道歉...
Based on shengteng AI Aibi intelligence, we launched a digital solution for bank outlets to achieve full digital coverage of information from headquarters to outlets
随机推荐
Activity enter exit animation
MFC宠物商店信息管理系统
埋点111
沟通的艺术III:看人之间 之倾听
How did automated specification inspection software develop?
2022年危险化学品经营单位主要负责人特种作业证考试题库及答案
uniapp
请问大佬们 有遇到过flink cdc mongdb 执行flinksql 遇到这样的问题的么?
“军备竞赛”时期的对比学习
Coneroller执行时候的-26374及-26377错误
DGL中异构图的一些理解以及异构图卷积HeteroGraphConv的用法
SQL Server 监控统计阻塞脚本信息
C language QQ chat room small project [complete source code]
使用bat命令一键启动常用浏览器
[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
GBase 8c数据库如何查看登录用户的登录信息,如上一次登录认证通过的日期、时间和IP等信息?
Nuxt//
运算符、、
Golang应用专题 - channel
QT implements JSON parsing