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

vue实现列表滑动下拉加载数据

一、实现效果

在这里插入图片描述

二、实现思路

使用滚动事件监听器来检测用户是否滚动到底部,然后加载更多数据

  1. 监听滚动事件。
  2. 检测用户是否滚动到底部。
  3. 加载更多数据。

三、案例代码

<div class="drawer-content"><div ref="loadMoreTrigger" class="load-more-trigger"></div><div v-if="isLoading" class="loading-button"><button type="primary" loading>加载中...</button></div>
</div>import { onBeforeUnmount, onMounted, ref, watch } from 'vue';const isLoading = ref(false);
const page = ref(1);const loadMoreData = async () => {if (isLoading.value) return;isLoading.value = true;// 模拟异步加载数据setTimeout(() => {const newItems: any = props.chartTableData?.slice(0, 10).map((item) => ({ ...item, id: item.id + page.value * 100 }));cardList.value = [...cardList.value, ...newItems];page.value += 1;isLoading.value = false;}, 1000);
};const handleScroll = () => {const drawerContent = document.querySelector('.drawer-content');if (drawerContent) {const { scrollTop, scrollHeight, clientHeight } = drawerContent;if (scrollTop + clientHeight >= scrollHeight - 10) {loadMoreData();}}
};onMounted(() => {const drawerContent = document.querySelector('.drawer-content');if (drawerContent) {drawerContent.addEventListener('scroll', handleScroll);}
});onBeforeUnmount(() => {const drawerContent = document.querySelector('.drawer-content');if (drawerContent) {drawerContent.removeEventListener('scroll', handleScroll);}
});.load-more-trigger {height: 1px;
}
.loading-button {text-align: center;margin-top: 10px;
}
http://www.lryc.cn/news/490312.html

相关文章:

  • 全面解析:HTML页面的加载全过程(四)--浏览器渲染之样式计算
  • #Verilog HDL# 谈谈代码中如何跨层次引用
  • LeetCode 每日一题 2024/11/18-2024/11/24
  • 客户流失分析综述
  • 基于51单片机的红包抽奖proteus仿真
  • cangjie (仓颉) vscode环境搭建
  • 阿里云私服地址
  • HTMLCSS:3D金字塔加载动画
  • shell编程(2)(3)
  • DFT专家分析scan insertion时使用EDT的策略
  • Apple Vision Pro开发003-PolySpatial2.0新建项目
  • 分公司如何纳税
  • 在 Ubuntu 系统上安装 npm 环境以及 nvm(Node Version Manager)
  • 深度优先搜索(dfs)题目合集
  • 性能监控利器:Ubuntu 22.04 上的 Zabbix 安装与配置指南
  • 性能测试的宏观分析:全面提升系统表现的关键
  • ctfshow
  • 【分享一个vue指令】鼠标放置提示指令v-tooltip
  • 掌握 Spring 事务管理:深入理解 @Transactional 注解
  • 字符三角形
  • 【LLM】一文学会SPPO
  • 如何通过ChatGPT提高自己的编程水平
  • NVR管理平台EasyNVR多品牌NVR管理工具的流媒体视频融合与汇聚管理方案
  • python之使用django框架开发web项目
  • ChatGPT 桌面版发布了,如何安装?
  • ubuntu 配置 多个 git 客户端 账户
  • React Native的界面与交互
  • autogen+ollama+litellm实现本地部署多代理智能体
  • InstantStyle容器构建指南
  • 百度主动推送可以提升抓取,它能提升索引量吗?