当前位置: 首页 > news >正文

虚拟列表的实现(简单易懂)

起因:

app开发过程中遇到需要渲染3000行的列表,页面直接卡顿,所以开始研究起虚拟列表

实现前提条件:

item项等高列表

实现思路:

首先是dom结构:

  1. 定义一个容器(固定高度),监听滚动事件
  2. 容器定义一个内部滚动容器,高度等于列表个数 * 每项高度
  3. 内部滚动容器里面再定义一个容器,具有transform: translateY属性,根据用户滚动动态改变位置,是真正展示的窗口
  4. 再里面就是展示的item项列表

然后是实现逻辑:

  1. 默认展示50条(条数不固定,保证展示能铺满屏幕即可),渲染完成后获取item项高度和最外面容器高度,根据它俩计算出应该展示的item项 Math.ceil(this.containerHeight / this.itemHeight)
  2. 用户滚动时在滚动事件里根据scrollTop设置translateY属性,用scrollTop % this.itemHeight去掉多余高度,保证会在边界上 translateY = scrollTop - (scrollTop % this.itemHeight)
  3. 用户滚动时在滚动事件里根据scrollTop和item项高度判断应该展示的起始项index startIndex = Math.floor(scrollTop / this.itemHeight)
  4. 最后重新渲染列表item项,渲染 startIndex + Math.ceil(this.boxHeight / this.itemHeight)之间的item项

代码展示:

<script lang="ts" setup name="virtual-list-comp">
import { onBeforeMount, ref, computed, nextTick, Ref, onMounted } from 'vue'
import { throttle } from '@/util/util'const props = defineProps({list: {type: Array,default: () => [],},
})// 容器高度
const containerHeight = ref(0)
// item项高度(默认取第一个)
const itemHeight = ref(0)
const getHeight = computed(() => {return itemHeight.value > 0 ? props.list.length * itemHeight.value + 'px' : 'auto'
})// 真正展示的列表
const showList: Ref<any[]> = ref([])
// 需要渲染的起始项
const startIndex = ref(0)
const itemNum = computed(() => {if (containerHeight.value && itemHeight.value) {return Math.ceil(containerHeight.value / itemHeight.value)}return 50
})
const translateY = ref(0)const initShowList = () => {showList.value = props.list.slice(startIndex.value, startIndex.value + itemNum.value)
}
// 获取盒子高度和item项高度
const initHeight = () => {containerHeight.value = document.querySelector('.virtual-list')?.clientHeight || 0itemHeight.value = document.querySelector('.virtual-list__item')?.clientHeight || 0
}const handleScroll = (e: any) => {const { scrollTop } = e.target// 获取 virtual-list__transform 这个展示视口的偏移值translateY.value = scrollTop - (scrollTop % itemHeight.value)startIndex.value = Math.floor(scrollTop / itemHeight.value)initShowList()
}onBeforeMount(async () => {initShowList()await nextTick()initHeight()
})
onMounted(() => {document.querySelector('.virtual-list__container')?.addEventListener('scroll', throttle(handleScroll, 10))
})
</script><template><div class="virtual-list h-full"><divclass="virtual-list__container overflow-y-scroll":style="{height: containerHeight + 'px',}"><div class="virtual-list__scroll" :style="{ height: getHeight }"><div class="virtual-list__transform" :style="{ transform: `translateY(${translateY}px)` }"><div v-for="(item, index) of showList" :key="index" class="virtual-list__item"><slot name="item" :item="item"></slot></div></div></div></div></div>
</template>
http://www.lryc.cn/news/116309.html

相关文章:

  • 【WordPress】如何在WordPress中实现真·页面路由
  • Android界面设计与用户体验
  • 基于 FFmpeg 的跨平台视频播放器简明教程(八):音画同步
  • 【NLP pytorch】基于BiLSTM-CRF模型医疗数据实体识别实战(项目详解)
  • 人工智能原理(1)
  • 预测成真,国内传来三个消息,中国年轻人变了,创新力产品崛起
  • 维深(Wellsenn):2023中国消费端VR内容开发商调研报告(附下载
  • redis事务管理详解
  • 国产低功耗蓝牙HS6621CxC/6621Px系列支持Find My网络功能方案芯片
  • 【openGauss】分区表的介绍与使用
  • 代码随想录算法训练营day57
  • 【基础类】—前后端通信类系统性学习
  • vite项目中使用@代表根路径
  • 冶金化工操作VR虚拟仿真实验软件提高员工们协同作业的配合度
  • SQL Server数据库 -- 索引与视图
  • 2023 java web面试秘籍
  • 2023-08-05力扣今日二题
  • stl_list类(使用+实现)(C++)
  • 利用hfish反控境外攻击源主机
  • 4、Rocketmq之存储原理
  • 在线原型设计工具有好用的吗?就是这10个
  • Vc - Qt - QPainter translate
  • Spark Catalog详解
  • 【Spring专题】手写简易Spring容器过程分析
  • fastadmin自定义键值组件Fieldlist
  • yolov2检测网数据集标注_labelme使用_json2txt格式转换
  • C/C++面试总结
  • Python爬虫的Selenium(学习于b站尚硅谷)
  • springboot 对接 minio 分布式文件系统
  • 前端小练习:案例4.3D图片旋转展示(旋转木马)