欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 时评 > 长列表局部渲染(监听window滚动),wndonw滚动同理

长列表局部渲染(监听window滚动),wndonw滚动同理

2025/3/24 6:19:01 来源:https://blog.csdn.net/u014071104/article/details/146393987  浏览:    关键词:长列表局部渲染(监听window滚动),wndonw滚动同理

场景

后端一股脑给了几千个数据,我们滚动过程永远只渲染20条

原理

滚动到底时候获取裁剪位置,的到需要渲染的20条数据,通过vue diff算法更新到dom

代码

<template><div class="container" @scroll="handleScroll"><div v-if="showRefreshHint" class="refresh-hint">下拉刷新...</div><ul><li v-for="(item, index) in visibleItems" :key="index" class="item">{{ item }}</li></ul><div v-if="loadingMore" class="loading">加载中...</div></div>
</template><script>
export default {data() {return {longArray: Array.from({ length: 1000 }, (_, i) => `Item ${i + 1}`), // 长数组visibleCount: 20, // 当前可见的条目数loadingMore: false, // 是否正在加载更多showRefreshHint: false, // 是否显示刷新提示lastScrollTop: 0, // 上一次的滚动位置};},computed: {visibleItems() {return this.longArray.slice(0, this.visibleCount);},},methods: {handleScroll(event) {const container = event.target;const { scrollTop, scrollHeight, clientHeight } = container;// 判断是否滚动到底部if (scrollTop + clientHeight >= scrollHeight - 10) {this.loadMore();}// 判断是否滚动到顶部if (scrollTop <= 0) {this.showRefreshHint = true;this.refreshData();} else {this.showRefreshHint = false;}// 保存当前滚动位置this.lastScrollTop = scrollTop;},loadMore() {if (this.loadingMore) return; // 防止重复加载// this.loadingMore = true;// 模拟异步加载// setTimeout(() => {this.visibleCount += 20; // 每次加载更多条目this.loadingMore = false;//}, 1000);},refreshData() {// 模拟刷新数据// setTimeout(() => {this.visibleCount = 20; // 重置可见条目数this.showRefreshHint = false; // 隐藏刷新提示// }, 1000);},},
};
</script><style>
.container {height: 100vh;overflow-y: auto;position: relative;background:#f3f2f2;
}
ul{padding:0;
}
.refresh-hint {text-align: center;padding: 10px;background-color: #f0f0f0;position: sticky;top: 0;z-index: 10;
}
.loading {text-align: center;padding: 10px;background-color: #f0f0f0;
}
.item {height: 60px;display: flex;align-items: center;padding: 0 10px;background:#fff;
}
.item +.item{margin-top:10px;
}
</style>

在线地址:

Vue SFC Playground

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词