当前位置:网站首页>上拉加载原理
上拉加载原理
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(‘触底’)
}
}
}
}
边栏推荐
- @Jsonadapter annotation usage
- Constrained layout flow
- GO项目实战 — Gorm格式化时间字段
- 分享.NET 轻量级的ORM
- > Could not create task ‘:app:MyTest. main()‘. > SourceSet with name ‘main‘ not found. Problem repair
- What is the most suitable book for programmers to engage in open source?
- In the year of "mutual entanglement" of mobile phone manufacturers, the "machine sea tactics" failed, and the "slow pace" playing method rose
- 2022年危险化学品经营单位主要负责人特种作业证考试题库及答案
- 《通信软件开发与应用》课程结业报告
- C language QQ chat room small project [complete source code]
猜你喜欢
Constrained layout flow
【js学习笔记五十四】BFC方式
爬虫(9) - Scrapy框架(1) | Scrapy 异步网络爬虫框架
> Could not create task ‘:app:MyTest. main()‘. > SourceSet with name ‘main‘ not found. Problem repair
WorkManager的学习二
Have you learned to make money in Dingding, enterprise micro and Feishu?
Learning note 4 -- Key Technologies of high-precision map (Part 2)
《天天数学》连载58:二月二十七日
双向RNN与堆叠的双向RNN
2022年危险化学品经营单位主要负责人特种作业证考试题库及答案
随机推荐
想请教一下,十大券商有哪些?在线开户是安全么?
pytorch输出tensor张量时有省略号的解决方案(将tensor完整输出)
A large number of virtual anchors in station B were collectively forced to refund: revenue evaporated, but they still owe station B; Jobs was posthumously awarded the U.S. presidential medal of freedo
vscode的快捷键
Pseudo class elements -- before and after
2022年流动式起重机司机考试题库及模拟考试
[paper reading] ckan: collaborative knowledge aware autonomous network for adviser systems
Today in history: the first e-book came out; The inventor of magnetic stripe card was born; The pioneer of handheld computer was born
【Vite】1371- 手把手开发 Vite 插件
重磅:国产IDE发布,由阿里研发,完全开源!
mongoDB副本集
What is the origin of the domain knowledge network that drives the new idea of manufacturing industry upgrading?
Ad20 make logo
函数///
web安全
AtCoder Beginner Contest 254「E bfs」「F st表维护差分数组gcd」
脚手架开发进阶
Atcoder beginer contest 254 "e BFS" f st table maintenance differential array GCD "
Blockbuster: the domestic IDE is released, developed by Alibaba, and is completely open source!
SLAM 01.人类识别环境&路径的模型建立