当前位置: 首页 > 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 开发的可复用卡片列表组件,支持以下功能:

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

相关文章:

  • [特殊字符] 分布式事务中,@GlobalTransactional 与 @Transactional 到底怎么配合用?
  • Python爬虫实战:获取xie程网敦煌景点数据,为51旅游路线做参考
  • Python实现图片浏览器
  • 连锁美业管理系统「数据分析」的重要作用分析︳博弈美业系统疗愈系统分享
  • 使用 Python 打造强大的文件分析工具
  • 海量粒子特效解决方案:VEG
  • java六人打分
  • 高效并发编程:无锁编程
  • 字节系a_bogus补环境
  • 浏览器相关知识点
  • 路由交换实验-手动聚合与LACP
  • 自动创建 中国古代故事人物一致性图画,看看扣子的空间是否能达到你的满意,自媒体的福音?
  • 【KWDB 创作者计划】_上位机知识篇---MicroPython
  • 一,开发环境安装
  • w~大模型~合集13
  • AUTODL关闭了程序内存依然占满怎么办
  • 〖 Linux 〗掌握 Linux 共享目录:权限、管理与最佳实践
  • 防火墙事件日志及日志分析
  • Python数据清洗笔记(上)
  • 文件内容隐写
  • 面向电力变压器的声纹智能诊断系统简析
  • Springfox + Swagger 的完整配置及同类框架对比的详细说明
  • (即插即用模块-特征处理部分) 四十一、(2024) MSAA 多尺度注意力聚合模块
  • 我的独立开发技术栈
  • 未曾设想的道路1
  • Ubuntu22.04新版本谷歌无法使用搜狗输入法/中文不显示
  • 三、Python编程基础03
  • 使用Python模拟子弹与子弹的碰撞
  • 四神-华夏大地的守护神
  • 【AI】MCP,弥补Agent的缺陷