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

虚拟列表

主要用到scrollTop、overflow:hidden、position:absolute

  1. 固定可视区域的列表高度和原始数据的总高度
  2. 获取scrollTop
  3. 根据scrollTop滚动条数,重新拿原数组中的数据
<template><div class="virtual-table" :style="{width: '600px',height: containerHeight + 'px',overflow: 'auto',}" @scroll="handleScroll"><div class="virtual-table-content" :style="{height: totalHeight + 'px',position: 'relative',}" ><div v-for="item in visibleData" :key="item.id":style="{height: itemHeight + 'px',position: 'absolute',top: (item.id * itemHeight) + 'px',left: 0,}"class="virtual-table-item">{{ item.name }} - {{ item.value }}</div></div></div>
</template><script setup>
import { ref, computed } from 'vue';const rowData = Array.from({ length: 10000 }, (_, i) => ({id: i,name: `Item ${i + 1}`,value: Math.floor(Math.random() * 1000),
}));const containerHeight = 400
const itemHeight = 50
const rowCount = Math.floor(containerHeight / itemHeight) + 1
let offsetY = ref(0)
const totalHeight = itemHeight * rowData.lengthconst visibleData = computed(() => {let startIndex = offsetY.value / itemHeightlet endIndex = startIndex + rowCountreturn rowData.slice(startIndex, endIndex)
})const handleScroll = function(e) {offsetY.value = e.target.scrollTop;
}
</script>
<style scoped>
.virtual-table-content {height: 100%;
}
</style>
http://www.rkmt.cn/news/1472.html

相关文章:

  • 条码控件Aspose.BarCode教程:使用 C# 构建 Code11 条形码生成器
  • 物理焦距、像素焦距、像元与相机内参(fx, fy)的意义与作用
  • java课前问题列表
  • vue3中两对容易搞混的概念
  • LoadRunner 对 WebTours 实现订票的性能分析
  • Python游戏开发:使用Pygame库的全面教程
  • 限行提醒小程序介绍
  • AP聚类算法实现三维数据点分类
  • 基于MATLAB的多输入多输出空时分组码通信系统仿真
  • 本土开发者生态崛起:Gitee如何重塑中国软件研发基础设施
  • .net code 连接SAP HANA 数据库
  • CentOS 上独立编译 Linux 内核一般性流程
  • VU9P板卡设计方案:565-基于VU9P的32@ SFP28+4@ QSFP28路光纤交换板卡
  • 黑产群控日损百万?设备ID乱象要如何终结?
  • 西门子分布式IO从站与主站的PN连接
  • Mysql:通用mysql备份脚本:纯shell实现:支持自动包含用户数据库、支持压缩、支持自动清理
  • JBOSS CVE-2017-7504 反序列化漏洞
  • 搞机工具箱v11.0.0免Root调试安卓
  • Gorm自定义数据类型
  • PHP流量控制令牌桶算法
  • 顽固文件夹无法删除?
  • c++ std::map
  • 软考架构备考-面向对象
  • 【Python】Word文档解析表格并导出Excel
  • 2025.9.9 总结
  • 2025 9 6 总结
  • OLE读取EXCEL内容
  • Rope pbds
  • 25.9.8随笔联考总结
  • IT 失业人员的福音:借微软 Dynamics 365 CRM 与 Power Platform 快速重启职业生涯