当前位置:网站首页>上拉加载原理
上拉加载原理
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(‘触底’)
}
}
}
}
边栏推荐
- DOM//
- Glide conclusion
- Implementation of wechat applet bottom loading and pull-down refresh
- "Everyday Mathematics" serial 58: February 27
- Go项目实战—参数绑定,类型转换
- Ad20 make logo
- C语言实现QQ聊天室小项目 [完整源码]
- Blockbuster: the domestic IDE is released, developed by Alibaba, and is completely open source!
- 2022年化工自动化控制仪表考试试题及在线模拟考试
- Learning note 4 -- Key Technologies of high-precision map (Part 2)
猜你喜欢

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

How to write high-quality code?

2022鹏城杯web

Events and bubbles in the applet of "wechat applet - Basics"

【黑马早报】罗永浩回应调侃东方甄选;董卿丈夫密春雷被执行超7亿;吉利正式收购魅族;华为发布问界M7;豆瓣为周杰伦专辑提前开分道歉...

How do programmers live as they like?

What is the most suitable book for programmers to engage in open source?

mongoDB副本集

Atcoder beginer contest 254 "e BFS" f st table maintenance differential array GCD "

2022年化工自动化控制仪表考试试题及在线模拟考试
随机推荐
@Jsonadapter annotation usage
Livedata interview question bank and answers -- 7 consecutive questions in livedata interview~
How can PostgreSQL CDC set a separate incremental mode, debezium snapshot. mo
WorkManager的学习二
TSQL–标示列、GUID 、序列
2022年流动式起重机司机考试题库及模拟考试
DDOS攻击原理,被ddos攻击的现象
Write double click event
Glide conclusion
@Serializedname annotation use
In wechat applet, after jumping from one page to another, I found that the page scrolled synchronously after returning
La vue latérale du cycle affiche cinq demi - écrans en dessous de cinq distributions moyennes
2022年化工自动化控制仪表考试试题及在线模拟考试
Interview: how does the list duplicate according to the attributes of the object?
【SWT组件】内容滚动组件 ScrolledComposite
IDEA新建sprintboot项目
【DNS】“Can‘t resolve host“ as non-root user, but works fine as root
Have the bosses ever encountered such problems in the implementation of flinksql by Flink CDC mongdb?
Pseudo class elements -- before and after
Taro进阶