vue3实现长列表虚拟滚动的示例代码
作者:jjw_zyfx
本文主要介绍了vue3实现长列表虚拟滚动的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
1、直接看代码
<template> <!--定义一个大容器,此容器可以滚动--> <div class="view" ref="viewRef" @scroll="handleScroll"> <!--定义一个可以撑满整个data的容器,主要是让父元素滚动起来--> <div class="phantom" :style="{ height: `${itemSize * data.length}px` }"></div> <!--显示元素列表 因为list也是绝对定位,当父元素进行滚动时,父元素需要平移来表示滚动(实际是看到的是没有滚动)--> <div class="list" :style="{ transform: `translateY(${translateLen}px)` }"> <!--显示每条数据--> <div v-for="item in visibleList" :style="{ height: `${itemSize}px` }"> {{ item }} </div> </div> </div> </template> <script setup> import {computed, reactive, ref} from 'vue'; //模拟请求回来的数据 const data = reactive((() => { const arr = []; for (let i = 0; i < 1000000; i++) arr[i] = i; return arr; })()); // 每条数据占据的高度像素值 const itemSize = 40 // 移动的距离 const translateLen = ref(0); // 绑定的元素 const viewRef = ref(null); // 从那条数据开始取值 const start = ref(0); // 可视区显示的条数 const visibleCount = computed(() => Math.ceil(viewRef.value?.clientHeight / itemSize)); // 可视区显示的数据内容(即需要渲染的所有数据内容) const visibleList = computed(() => data.slice(start.value, start.value + visibleCount.value)); // 滚动起来后需要处理的显示的开始位置和要移动的距离 const handleScroll = () => { const scrollTop = viewRef.value.scrollTop; start.value = Math.floor(scrollTop / itemSize); console.log("scrollTop==>", scrollTop) translateLen.value = scrollTop console.log("translateLen.value==>", translateLen.value) } </script> <style scoped> .view { position: relative; height: 400px; width: 400px; overflow: auto; } .phantom { /* 必须要绝对定位 */ position: absolute; width: 100%; background-color: pink; } .list { /* 必须要绝对定位 */ position: absolute; } </style>
显示的结果如下:
当向下滚动时:
再向上滚动:
到此这篇关于vue3实现长列表虚拟滚动的示例代码的文章就介绍到这了,更多相关vue3 长列表虚拟滚动内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!