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

基于 Vue 2 开发的分页卡片列表组件(带懒加载和点击事件)

功能目标:

  1. CardList.vue 中支持分页,每页显示指定数量的卡片。
  2. 添加“加载中”动画。
  3. 支持懒加载:滚动到底部自动加载下一页。
  4. 点击卡片的事件逻辑由 Card.vue 内部发出,并由 CardList 向上传递。

主页面文件 Home.vue

<template><div><h1>Card List 示例</h1><CardList :dataList="cards" @card-click="handleCardClick" /></div>
</template><script>
import CardList from './components/CardList.vue'export default {name: 'Home',components: {CardList},data() {return {// 假设这是全部数据(实际应用中可从API分页加载)cards: Array.from({ length: 50 }, (_, i) => ({id: i + 1,title: `Card #${i + 1}`,description: `This is card number ${i + 1}.`}))}},methods: {handleCardClick(card) {alert(`你点击了: ${card.title}`)}}
}
</script>

CardList.vue (分页 + 懒加载 + 加载动画)

<template><div class="card-list"><Cardv-for="item in paginatedList":key="item.id":cardData="item"@card-click="handleCardClick"/><div v-if="loading" class="loading">加载中...</div></div>
</template><script>
import Card from './Card.vue'export default {name: 'CardList',components: { Card },props: {dataList: {type: Array,required: true}},data() {return {pageSize: 10,currentPage: 1,loading: false}},computed: {paginatedList() {return this.dataList.slice(0, this.currentPage * this.pageSize)}},mounted() {window.addEventListener('scroll', this.onScroll)},beforeDestroy() {window.removeEventListener('scroll', this.onScroll)},methods: {handleCardClick(card) {this.$emit('card-click', card)},onScroll() {const scrollBottom = window.innerHeight + window.scrollY >= document.body.offsetHeight - 10if (scrollBottom && !this.loading && this.canLoadMore) {this.loadNextPage()}},loadNextPage() {this.loading = truesetTimeout(() => {this.currentPage++this.loading = false}, 1000) // 模拟加载延迟}},computed: {canLoadMore() {return this.paginatedList.length < this.dataList.length}}
}
</script><style scoped>
.card-list {display: flex;flex-wrap: wrap;gap: 16px;
}.loading {width: 100%;text-align: center;padding: 16px;color: #999;font-style: italic;
}
</style>

Card.vue (点击内部触发)

<template><div class="card" @click="handleClick"><h3>{{ cardData.title }}</h3><p>{{ cardData.description }}</p></div>
</template><script>
export default {name: 'Card',props: {cardData: {type: Object,required: true}},methods: {handleClick() {this.$emit('card-click', this.cardData)}}
}
</script><style scoped>
.card {border: 1px solid #ccc;padding: 16px;border-radius: 8px;cursor: pointer;width: 200px;transition: box-shadow 0.2s;
}
.card:hover {box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
}
</style>

总结:

这是一个基于 Vue 2 开发的可复用卡片列表组件,支持以下功能:

  • 分页加载:初始显示部分数据,滚动到底部自动加载更多;
  • 懒加载机制:通过监听页面滚动事件实现无限加载;
  • 点击交互:每个卡片可点击,事件由卡片内部触发并向上传递;
  • 加载状态显示:加载新数据时展示“加载中…”提示;

相关文章:

  • 对流对象的理解
  • 知识储备-DC综合相关
  • 新手村:过拟合(Overfitting)
  • # 深度学习中的学习率调度:以 PyTorch 图像分类为例
  • Java 开发瓶颈破局:飞算 JavaAI 如何一站式生成标准化项目结构?
  • 云贝餐饮 最新 V3 独立连锁版 全开源 多端源码 VUE 可二开
  • C++面向对象特性之继承篇
  • 生物计算安全攻防战:从DNA存储破译到碳基芯片防御体系重构
  • PowerQuery汇总整个文件夹中的数据
  • DC-2寻找Flag1、2、3、4、5,wpscan爆破、git提权
  • python:mido 提取 midi文件中某一音轨的音乐数据
  • 容器修仙传 我的灵根是Pod 第7章 傀儡秘术(StatefulSet)
  • 电控---DMP库
  • Java 加密与解密:从算法到应用的全面解析
  • 深入解析 Linux 系统中库的加载机制:从静态链接到动态运行时
  • 序章:写在前面
  • 进行网页开发时,怎样把function()中变量值在控制台输出,查看?
  • 意见反馈留言二维码制作
  • neo4j中节点内的名称显示不全解决办法(如何让label在节点上自动换行)
  • Discuz!与DeepSeek的AI融合:打造智能网址导航新体验——以“虎跃办公”为例
  • “全国十大考古”揭晓:盘龙城遗址、周原遗址入围
  • 生态环境部:我国核电规模全球第一,总体安全可控
  • 上海市长会见璞跃全球创始人亚美迪,建设国际AI创新创业网络中心节点
  • 外卖江湖战火重燃,骑手、商家、消费者在“摇摆”什么?
  • 正荣地产旗下“H20正荣2”债未能于宽限期内支付分期偿付款,尚未就新兑付方案达成一致
  • 从香料到文化,跟着陈晓卿寻味厦门