vue-监听视图滚动-加载下一页数据(鼠标滚动到底,加载)
1 2 3 4 5 6 7 | < div class="daily-list" ref="list"> < template > < div v-for="(item,index) in arrayItem" :key="index"> < div >{{item}}</ div > </ div > </ template > </ div > |
data:
arrayItem: 100,
isLoading: false
style:
.daily-list{
width: 300px;
position: fixed;
top: 0;
bottom: 0;
left: 150px;
/* 具备滚动的能力 */
overflow: auto;
border-right: 1px solid #d7dde4;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | mounted() { // 获取dom const $list = this .$refs.list; // 监听内容的滚动事件 $list.addEventListener( 'scroll' , () => { if ( this .isLoading) return ; // 已经滚动的距离加页面的高度,等于整个内容区域高度时,视为接触底部 console.log( '已经滚动距离' ,$list.scrollTop); console.log( '页面的高度' ,document.body.clientHeight); console.log( '内容区域高度' ,$list.scrollHeight); if ( $list.scrollTop + document.body.clientHeight >= $list.scrollHeight ) { console.log( '到底了' ); this .isLoading = true ; setTimeout(()=>{ this .arrayItem = 200; this .isLoading = false ; },2000) } }); }, |
或者
<div class="daily-list" ref="list" @scroll="handleScroll">
<template>
<div v-for="(item,index) in arrayItem" :key="index">
<div>{{item}}</div>
</div>
</template>
</div>
methods: {
handleScroll(){
const $list = this.$refs.list;
if (this.isLoading) return;
if
(
$list.scrollTop
+ document.body.clientHeight
>= $list.scrollHeight
)
{
console.log('到底了');
this.isLoading = true;
setTimeout(()=>{
this.arrayItem = 200;
this.isLoading = false;
},2000)
}
}
}

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?