当前位置:网站首页>浏览器滚动加载更多实现
浏览器滚动加载更多实现
2022-07-02 06:22:00 【秦时明月之安康】
一、代码实现
下面的代码是基于我常用的UI框架vue实现的,如果你是其他框架,直接提取滚动的关键代码即可,注意 crollPoint 加了0.5是为了兼容企业微信内置浏览器,当企业微信内置浏览器首次滚动到底部的时候,变量 crollPoint 和 totalPageHeight
值如下:
| TYPE | ASCII |
|---|---|
| scrollPoint | 1600.6666870117188 |
| Quotes | 1601 |
可以看到如果不加 0.5, scrollPoint >= totalPageHeight 是不成立的,也就无法滚动加载了。
<template>
<div></div>
</template>
<script>
export default {
data() {
return {
data: [],
oriData: [],
page: {
total: 0,
pageSize: 10, // 滚动一次加载10条数据
current: 1,
},
};
},
mounted() {
document.addEventListener('scroll', this.scrollMoreData, false);
},
destroyed() {
document.removeEventListener('scroll', this.scrollMoreData, false);
},
methods: {
scrollMoreData() {
const {
total, current, pageSize } = this.page;
// @var int totalPageHeight
const totalPageHeight = document.body.scrollHeight;
// @var int scrollPoint
const scrollPoint = window.scrollY + window.innerHeight;
// check if we hit the bottom of the page
console.log('scrollPoint >= totalPageHeight', scrollPoint, totalPageHeight);
if (scrollPoint + 0.5 >= totalPageHeight && current * pageSize <= total) {
console.log('at the bottom');
this.data = this.data.concat(this.oriData.slice(current * pageSize, pageSize * (current + 1)));
this.page.current += 1;
}
},
},
};
</script>
<style lang="less" scoped></style>
二、参考文档
Javascript: How to detect if browser window is scrolled to bottom?
边栏推荐
猜你喜欢

Sparse array (nonlinear structure)

Find the highest value of the current element Z-index of the page

Redis - cluster data distribution algorithm & hash slot

Cglib agent - Code enhancement test

ctf三计

ZZQ的博客目录--更新于20210601

Summary of WLAN related knowledge points

ctf-web之练习赛

unittest. Texttestrunner does not generate TXT test reports

qq邮箱接收不到jenkins构建后使用email extension 发送的邮件(timestamp 或 auth.......)
随机推荐
Self cultivation of programmers - Reflection on job hunting
利用NVIDIA GPU将Minecraft场景渲染成真实场景
Data science [viii]: SVD (I)
kali最新更新指南
记录一次RDS故障排除--RDS容量徒增
【张三学C语言之】—深入理解数据存储
Amazon AWS data Lake Work Pit 1
FE - Weex 使用简单封装数据加载插件为全局加载方法
CUDA中的动态全局内存分配和操作
构建学习tensorflow
Redis - cluster data distribution algorithm & hash slot
CUDA user object
Introduce two automatic code generators to help improve work efficiency
查询GPU时无进程运行,但是显存却被占用了
看完有用的blog
web自动化切换窗口时报错“list“ object is not callable
unittest. Texttestrunner does not generate TXT test reports
程序员的自我修养—找工作反思篇
广告业务Bug复盘总结
RestTemplate请求时设置请求头,请求参数,请求体。