虚拟滚动列表(每一项固定高度)

一、概述

在Web开发中,当需要展示大量数据时,直接渲染所有数据会导致页面性能下降,尤其是在列表项较多的情况下。虚拟滚动(Virtual Scrolling)是一种优化技术,它通过只渲染当前可见的列表项来减少DOM节点的数量,从而提高页面的渲染性能。

本文将介绍如何实现一个简单的虚拟滚动列表,其中每个列表项的高度是固定的。我们将使用Vue.js框架来实现这一功能,并结合HTML和CSS来完成布局和样式。

二、效果图

三、具体实现

html 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>虚拟滚动列表(每一项固定高度)</title>
  <style>
    .virtual-scroll-container {
      height: 300px;
      overflow-y: auto;
      border: 1px solid #ccc;
      position: relative;
    }

    .virtual-scroll-list {
      position: relative;
    }

    .virtual-scroll-item {
      position: absolute;
      width: 100%;
      box-sizing: border-box;
      border-bottom: 1px solid #eee;
      padding: 10px;
    }
  </style>
</head>

<body>
  <div id="app">
    <h1>虚拟滚动列表Demo</h1>
    <div class="virtual-scroll-container" @scroll="handleScroll">
      <div class="virtual-scroll-list" :style="{ height: totalHeight + 'px' }">
        <div class="virtual-scroll-item" v-for="item in visibleItems" :key="item.id"
          :style="{ top: item.top + 'px', height: itemHeight + 'px' }">
          {{ item.content }}
        </div>
      </div>
    </div>
  </div>

  <script src="https://lf26-cdn-tos.bytecdntp.com/cdn/expire-1-M/vue/2.6.14/vue.min.js"></script>
  <script>
    new Vue({
      el: '#app',
      data() {
        return {
          items: [], // 数据列表
          itemHeight: 50, // 每个项的高度
          visibleCount: 10, // 可见的项数
          scrollTop: 0, // 滚动位置
        };
      },
      computed: {
        // 计算总高度
        totalHeight() {
          return this.items.length * this.itemHeight;
        },
        // 计算起始索引
        startIndex() {
          return Math.floor(this.scrollTop / this.itemHeight);
        },
        // 计算结束索引
        endIndex() {
          return Math.min(
            this.startIndex + this.visibleCount,
            this.items.length - 1
          );
        },
        // 计算可见的项
        visibleItems() {
          return this.items
            .slice(this.startIndex, this.endIndex + 1)
            .map((item, index) => ({
              ...item,
              top: (this.startIndex + index) * this.itemHeight,
            }));
        },
      },
      methods: {
        // 处理滚动事件
        handleScroll(event) {
          this.scrollTop = event.target.scrollTop;
        },
      },
      mounted() {
        // 初始化数据
        for (let i = 0; i < 1000; i++) {
          this.items.push({
            id: i,
            content: `Item ${i + 1} -+-+-+-测试数据-+-+-+-`,
          });
        }
      },
    });
  </script>
</body>

</html>
相关推荐
UXbot7 分钟前
AI原型设计工具评测:从创意到交互式Demo,5款产品全面解析
前端·ui·设计模式·ai·ai编程·原型模式
落魄江湖行8 分钟前
硅基同事埋的坑,我用2小时才填平:Nuxt 4 路由踩坑:可选参数 [[id]] 与 [id] 的区别
前端
一勺菠萝丶14 分钟前
管理后台使用手册在线预览与首次登录引导弹窗实现
java·前端·数据库
军军君0114 分钟前
Three.js基础功能学习十四:智能黑板实现实例一
前端·javascript·css·typescript·前端框架·threejs·智能黑板
小村儿16 分钟前
连载05-Claude Skill 不是抄模板:真正管用的 Skill,都是从实战里提炼出来的
前端·后端·ai编程
xiaotao13123 分钟前
JS new 操作符完整执行过程
开发语言·前端·javascript·原型模式
robch29 分钟前
python3 -m http.server 8001直接启动web服务类似 nginx
前端·nginx·http
吴声子夜歌35 分钟前
ES6——数组的扩展详解
前端·javascript·es6
guhy fighting44 分钟前
new Map,Array.from,Object.entries的作用以及使用方法
开发语言·前端·javascript
大漠_w3cpluscom44 分钟前
CSS 技巧:CSS 单位使用指南
前端