当前位置:网站首页>上拉加载原理
上拉加载原理
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(‘触底’)
}
}
}
}
边栏推荐
- AtCoder Beginner Contest 254「E bfs」「F st表维护差分数组gcd」
- Qt实现json解析
- 各位大佬,我测试起了3条线程同时往3个mysql表中写入,每条线程分别写入100000条数据,用了f
- How do programmers live as they like?
- 非技術部門,如何參與 DevOps?
- C function returns multiple value methods
- NAS与SAN
- Livedata interview question bank and answers -- 7 consecutive questions in livedata interview~
- [vite] 1371 - develop vite plug-ins by hand
- Interview: how does the list duplicate according to the attributes of the object?
猜你喜欢

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

Solution of ellipsis when pytorch outputs tensor (output tensor completely)

Constrained layout flow
![[observation] with the rise of the](/img/9a/8bbf98e6aed80638f4340aacec2ea9.jpg)
[observation] with the rise of the "independent station" model of cross-border e-commerce, how to seize the next dividend explosion era?

【Vite】1371- 手把手开发 Vite 插件

手机厂商“互卷”之年:“机海战术”失灵,“慢节奏”打法崛起

SAP ui5 objectpagelayout control usage sharing
![[vite] 1371 - develop vite plug-ins by hand](/img/7f/84bba39965b4116f20b1cf8211f70a.png)
[vite] 1371 - develop vite plug-ins by hand

"Everyday Mathematics" serial 58: February 27

伪类元素--before和after
随机推荐
微信小程序中,从一个页面跳转到另一个页面后,在返回后发现页面同步滚动了
TypeError: Cannot read properties of undefined (reading ‘cancelToken‘)
[paper reading] kgat: knowledge graph attention network for recommendation
Go项目实战—参数绑定,类型转换
《天天数学》连载58:二月二十七日
Nine degrees 1480: maximum ascending subsequence sum (dynamic programming idea for the maximum value)
Learning Note 6 - satellite positioning technology (Part 1)
Events and bubbles in the applet of "wechat applet - Basics"
2022年流动式起重机司机考试题库及模拟考试
到底谁才是“良心”国产品牌?
AtCoder Beginner Contest 258「ABCDEFG」
Customize the left sliding button in the line in the applet, which is similar to the QQ and Wx message interface
微信核酸检测预约小程序系统毕业设计毕设(7)中期检查报告
Interview: is bitmap pixel memory allocated in heap memory or native
AtCoder Beginner Contest 258「ABCDEFG」
【tcp】服务器上tcp连接状态json形式输出
字符串、、
爬虫(9) - Scrapy框架(1) | Scrapy 异步网络爬虫框架
SQL Server 监控统计阻塞脚本信息
2022年危险化学品经营单位主要负责人特种作业证考试题库及答案