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

长列表局部渲染(监听window滚动),wndonw滚动同理

场景

后端一股脑给了几千个数据,我们滚动过程永远只渲染20条

原理

滚动到底时候获取裁剪位置,的到需要渲染的20条数据,通过vue diff算法更新到dom

代码

<template>
  <div class="container" @scroll="handleScroll">
    <div v-if="showRefreshHint" class="refresh-hint">下拉刷新...</div>
    <ul>
      <li v-for="(item, index) in visibleItems" :key="index" class="item">
        {{ item }}
      </li>
    </ul>
    <div v-if="loadingMore" class="loading">加载中...</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      longArray: Array.from({ length: 1000 }, (_, i) => `Item ${i + 1}`), // 长数组
      visibleCount: 20, // 当前可见的条目数
      loadingMore: false, // 是否正在加载更多
      showRefreshHint: false, // 是否显示刷新提示
      lastScrollTop: 0, // 上一次的滚动位置
    };
  },
  computed: {
    visibleItems() {
      return this.longArray.slice(0, this.visibleCount);
    },
  },
  methods: {
    handleScroll(event) {
      const container = event.target;
      const { scrollTop, scrollHeight, clientHeight } = container;

      // 判断是否滚动到底部
      if (scrollTop + clientHeight >= scrollHeight - 10) {
        this.loadMore();
      }

      // 判断是否滚动到顶部
      if (scrollTop <= 0) {
        this.showRefreshHint = true;
        this.refreshData();
      } else {
        this.showRefreshHint = false;
      }

      // 保存当前滚动位置
      this.lastScrollTop = scrollTop;
    },
    loadMore() {
      if (this.loadingMore) return; // 防止重复加载
     // this.loadingMore = true;

      // 模拟异步加载
     // setTimeout(() => {
        this.visibleCount += 20; // 每次加载更多条目
        this.loadingMore = false;
      //}, 1000);
    },
    refreshData() {
      // 模拟刷新数据
     // setTimeout(() => {
        this.visibleCount = 20; // 重置可见条目数
        this.showRefreshHint = false; // 隐藏刷新提示
     // }, 1000);
    },
  },
};
</script>

<style>
.container {
  height: 100vh;
  overflow-y: auto;
  position: relative;
  background:#f3f2f2;
}
ul{
  padding:0;
}
.refresh-hint {
  text-align: center;
  padding: 10px;
  background-color: #f0f0f0;
  position: sticky;
  top: 0;
  z-index: 10;
}
.loading {
  text-align: center;
  padding: 10px;
  background-color: #f0f0f0;
}
.item {
  height: 60px;
  display: flex;
  align-items: center;
  padding: 0 10px;
  background:#fff;
}
.item +.item{
  margin-top:10px;
}
</style>

在线地址:

Vue SFC Playground


http://www.kler.cn/a/595866.html

相关文章:

  • 注意力机制:让AI拥有黄金七秒记忆的魔法--(注意力机制中的Q、K、V)
  • 广度优先搜索(BFS)完全解析:从原理到 Java 实战
  • 分布式中间件:RabbitMQ确认消费机制
  • Ubuntu 22.04 上配置 ufw(Uncomplicated Firewall)防火墙的详细步骤
  • watch方法解析
  • win32汇编环境,网络编程入门之八
  • 20250319在荣品的PRO-RK3566开发板的buildroot系统下使用集成的QT应用调试串口UART3
  • 深度学习与计算机视觉方向
  • docker、docker-compose常用命令
  • 【C#高级编程】—表达式树详解
  • k8s自动弹性伸缩之HPA实践
  • 网络编程——套接字、创建服务器、创建客户端
  • 挑战用AI替代我的工作——从抢券困境到技术突破
  • C# System.Text.Encoding 使用详解
  • 支持向量机(Support Vector Machine)基础知识1
  • 普通鼠标的500连击的工具来了!!!
  • 【AIGC】Win10系统极速部署Docker+Ragflow+Dify
  • 最新!Ubuntu Docker 安装教程
  • C 语 言 --- 扫 雷 游 戏(初 阶 版)
  • 系统思考—链接组织效能提升与问题解决