当前位置: 首页 > 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.wxhsa.cn/company.asp?id=50

相关文章:

  • 条码控件Aspose.BarCode教程:使用 C# 构建 Code11 条形码生成器
  • 智能引擎驱动软件工业化革命:Gitee PPM如何重塑企业级开发范式
  • 深入解析Web Components:Shadow DOM实战指南
  • HCIP回顾— BGP基础
  • 你的测试又慢又不可靠-因为你测错了东西
  • 你的错误处理一团糟-是时候修复它了-️
  • 物理焦距、像素焦距、像元与相机内参(fx, fy)的意义与作用
  • 实时通信的头痛-问题不在WebSocket而是你的框架
  • 文件不只是数据-一份稳健的文件处理指南
  • vue+websocket+Stomp组件实现前端长连接
  • java课前问题列表
  • 多字段排序工具类,支持树形
  • 鸿蒙 HAP 包处理全攻略:从解包到签名,So 库加固一步到位
  • 关于vue在PC端,rem对不同屏幕进行适配
  • GreatSQL分页查询优化案例实战
  • 技术面:Java并发(线程同步、死锁、多线程编排)
  • vue3中两对容易搞混的概念
  • LoadRunner 对 WebTours 实现订票的性能分析
  • mac一键关闭chrome自动更新
  • Python游戏开发:使用Pygame库的全面教程
  • 同城黑卡小程序系统介绍
  • 限行提醒小程序介绍
  • 365 快乐农场小程序介绍
  • AP聚类算法实现三维数据点分类
  • 政务预约系统介绍
  • 23Java基础之File
  • 猜灯谜赢大奖系统介绍
  • Linux GNU 工具集详解
  • 基于MATLAB的多输入多输出空时分组码通信系统仿真
  • 国产DevOps工具链崛起:Gitee如何重塑企业研发效能版图