首页 > 其他分享 >vue 实现虚拟列表

vue 实现虚拟列表

时间:2022-12-16 23:55:39浏览次数:35  
标签:vue const value 列表 startIndex 虚拟 props ref size


// 调用组件
<List :arr="dataList">
  <template #default="{item}">
    {{item * 3}}
  </template>
</List>


// 虚拟列表组件
<template>
  <div class=''>
    <!-- 给用户看到的20个元素的容器 -->
    <div class="view_port" ref="viewPortRef" @scroll="changeScroll">
      <!-- 为了撑开容器 -->
      <div class="scroll_bar" ref="scrollBarRef"></div>
      <!-- 虚拟的列表 -->
      <div class="list" :style="{ transform: `translate(0, ${offsetTop}px)` }">
        <div :style="{ 'height': size + 'px' }" v-for="(item, index) in itemsList" :key="index">
          <slot :item="item">{{item}}</slot>
        </div>
      </div>
    </div>
  </div>
</template>
<script setup lang='ts'>
import { withDefaults, defineProps, ref, onMounted, computed } from "vue";

const props = withDefaults(defineProps<{
  arr: Array<number>, // 需要滚动的数据
  remain?: number, // 每个元素的高度
  size?: number // 视口展示元素的数量
}>(), {
  remain: 20,
  size: 20
})

const viewPortRef = ref<HTMLElement>();
const scrollBarRef = ref<HTMLElement>();

const initPage = () => {
  // 设置视口的高度
  viewPortRef.value!.style.height = props.remain * props.size + "px";
  // 设置整个滚动条的高度
  scrollBarRef.value!.style.height = props.arr.length * props.size + 'px';
}

const startIndex = ref(0);
const endIndex = ref(props.size);
const offsetTop = ref(0);

// 用于获取真实得到的列表
const itemsList = computed(() => props.arr.slice(startIndex.value, endIndex.value))

// 监听页面的滚动
const changeScroll = () => {
  // 计算向上卷入的元素的开始下标,乡下取整
  startIndex.value = Math.floor(viewPortRef.value!.scrollTop / props.size);
  // 计算截取的元素结束下标
  endIndex.value = startIndex.value + props.size;

  // 可视高度向下偏移的数量
  offsetTop.value = startIndex.value * props.size
}


onMounted(() => {
  initPage();

})

</script>
<style scoped>
.view_port {
  overflow-y: scroll;
  position: relative;
}

.list {
  position: absolute;
  top: 0;
  left: 0;
}
</style>

标签:vue,const,value,列表,startIndex,虚拟,props,ref,size
From: https://www.cnblogs.com/rzl795/p/16988514.html

相关文章

  • VUE数据双向绑定
    5.Vue数据双向绑定5.1.什么是双向数据绑定Vue.js是一个MVVM框架,即数据双向绑定,即当数据发生变化的时候,视图也就发生变化,当视图发生变化的时候,数据也会跟着同步变......
  • 前言 --- 安装虚拟机及CentOS 7
      前言 ---  安装虚拟机及CentOS7一、CentOS7版本安装(解压安装)镜像链接:​​https://pan.baidu.com/s/1E-L-O74flvloYVwI9Oi7Vg​​提取码:eyn3 注释:镜像文件​​.......
  • hualinux 进阶 vue 5.0:vue UI组件介绍及Element UI
    目录​​ 一、vue流行的UI介绍​​​​1.1un-app所有前端框架​​​​1.2pc浏览器​​​​1.3 小程序mpvue​​​​1.4移动端​​​​1.5 桌面端(客户端)Electron ​​......
  • Vue键盘事件
     Vue中常用的按键别名:              回车=>enter              删除=>delete(捕获“删除”和“退格”......
  • 成员初始化列表
    成员初始化列表的概念在类的构造函数中,通过在构造函数的括号和花括号之间使用冒号和成员变量初始化列表进行初始化,而不是在函数体对成员变量进行初始化。注意:初始化顺序......
  • Java虚拟机定义
    虚拟机是一种抽象化的​​计算机​​​,通过在实际的计算机上仿真模拟各种计算机功能来实现的。Java虚拟机有自己完善的​​​硬体​​​架构,如​​​处理器​​......
  • tunctl 创建虚拟网卡
    安装[root@]#rpm-ivhhttp://li.nux.ro/download/nux/misc/el7/x86_64/tunctl-1.5-12.el7.nux.x86_64.rpm参数:-u指定用户名,表明这个接口只受该用户控制,这个接口发......
  • Azure 解决方案:如何通过Network Peering架构两个虚拟网络的连接
    51CTO博客地址:​ ​​​​https://blog.51cto.com/14669127​​Azure培训视频地址:​ ​​​https://space.bilibili.com/2000820534​​如果客户在AzureVM上部署了Local......
  • vue3中使用vite-ts构建项目时tsconfig.json的配置
    在上一次创建vue3项目在tsconfig.json中配置了文件别名以后,格式校验提示   es3什么鬼,便去看了一下tsconfig.json的配置,以此学习{"compilerOptions":{......
  • 企业级自定义表单引擎解决方案(十八)--列表视图属性设置
    表格对于后台管理类的系统来说,至关重要,系统大多数功能都需要以表格的方式展示业务内容,系统开发人员多数时间也是围绕着表格进行业务编码,接触过很多后台管理系统的框架,我个......