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

封装触底加载组件

 (1)首先创建一个文件名为:InfiniteScroll.vue

<template>
	<div ref="scrollContainer" class="infinite-scroll-container">
		<slot></slot>
		<div v-if="loading" class="loading-spinner">加载中...</div>
	</div>
</template>

<script setup>
	import {
		ref,
		onMounted,
		onUnmounted,
		defineProps
	} from 'vue';

	const props = defineProps({
		loadMore: {
			type: Function,
			required: true
		},
		loading: {
			type: Boolean,
			default: false
		}
	});

	const scrollContainer = ref(null);

	const handleScroll = () => {
		if (!scrollContainer.value) return;

		const container = scrollContainer.value;
		const containerHeight = container.clientHeight;
		const scrollTop = container.scrollTop;
		const contentHeight = container.scrollHeight;

		if (scrollTop + containerHeight >= contentHeight - 10) {
			//触发逻辑
			if (!props.loading) {
				props.loadMore();
			}
		}
	};

	onMounted(() => {
		if (scrollContainer.value) {
			scrollContainer.value.addEventListener('scroll', handleScroll);
		}
	});

	onUnmounted(() => {
		if (scrollContainer.value) {
			scrollContainer.value.removeEventListener('scroll', handleScroll);
		}
	});
</script>

<style scoped>
	.infinite-scroll-container {
		overflow-y: auto;
		position: relative;
	}

	.loading-spinner {
		text-align: center;
		padding: 10px;
	}
</style>

(2)如何使用

<template>
  <InfiniteScroll :loadMore="loadMoreData" :loading="loading">
    <!-- 列表内容 -->
    <div v-if="isEnd" class="end-message">已经到底了</div>
  </InfiniteScroll>
</template>

<style scoped>
.end-message {
  text-align: center;
  padding: 10px;
  color: #888;
}
</style>

示例:

<InfiniteScroll :loadMore="loadMoreData" class="shellwantbuy-center" v-if="buylstData.length>0">
	<div class="shellwantbuy-center-item" v-for="(item,index) in buylstData" :key="item">
        内容展示
		</div>
		<div v-if="isEnd" class="end-message">已经到底了</div>
	<div v-if="loading" class="loading-spinner">加载中...</div>
</InfiniteScroll>



<script setup>
import {
		Tradewanttobuypull,
		Tradewanttobuyuserlst
	} from '@/api/modules/secondarymMarket'
	import InfiniteScroll from '@/components/InfiniteScroll.vue'
	import {
		onMounted,
		ref,
		watch
	} from 'vue'
	import empyt from '@/components/EmptyState.vue'
	const buylstData = ref([])
	const token = uni.getStorageSync('token')
	const page = ref(1);
	const pageSize = 10;
	const loading = ref(false);
	const last_page = ref()
	const isEnd = ref(false);

	const loadMoreData = async () => {
		if (loading.value) return;

		loading.value = true;
		try {
			const data = await Tradewanttobuyuserlst({
				page_size: pageSize,
				page: page.value,
				type: '1',
				token,
			});
			const buydata = data;

			last_page.value = buydata.data.last_page
			
			console.log(page.value,last_page.value)
			if (page.value <= last_page.value) {
				buylstData.value.push(...buydata.data.data);
				page.value++;
			} else {
				isEnd.value = true;
			}
		} catch (error) {
			console.error('Failed to load data:', error);
		} finally {
			loading.value = false;
		}
	};

	onMounted(() => {
		loadMoreData()
	})
</script>


http://www.kler.cn/news/295152.html

相关文章:

  • ✨机器学习笔记(一)—— 监督学习和无监督学习
  • 包机制,javadoc生成文档,用户交互scanner
  • 怎样通过STM32实现环境监测设计
  • 【大数据分析与挖掘算法】matlab实现——Apriori关联规则算法
  • 一篇文章告诉你小程序为什么最近这么火?
  • mysql创建新表,同步数据
  • 简单实用的php全新实物商城系统
  • 2024国赛数学建模B题完整分析参考论文38页(含模型和可运行代码)
  • 深度学习速通系列:如何生成句向量?
  • 9.8通宵速通javascript
  • [论文笔记]QLoRA: Efficient Finetuning of Quantized LLMs
  • 系统架构师考试学习笔记第三篇——架构设计高级知识(19)嵌入式系统架构设计理论与实践
  • 3177. 求出最长好子序列 II / 3176. 求出最长好子序列 I(24.9.7 / 24.9.8)
  • pdf转word格式乱了怎么调整?2024帮助你快速进行pdf格式调整的软件
  • [论文笔记]Circle Loss: A Unified Perspective of Pair Similarity Optimization
  • Nginx跨域运行案例:云台控制http请求,通过 http server 代理转发功能,实现跨域运行。(基于大华摄像头WEB无插件开发包)
  • 4K4D: Real-Time 4D View Synthesis at 4K Resolution 学习笔记
  • 什么是 Java?Java 的主要特点有哪些?
  • 调度器怎么自己写?调度器在实现时需要注意哪些细节?请写一个jvm的调度器?如何在这个调度器中添加多个任务?
  • Windows下Python和PyCharm的应用(六)__应用Opencv的第一个程序(图片载入)
  • 2024/9/6黑马头条跟学笔记(四)
  • STM32的GPIO使用
  • QT定时器QObiect/QTimer
  • 【环境领域EI稳定 I 院士主讲】第九届能源与环境研究进展国际学术会议(ICAEER 2024)
  • 【H2O2|全栈】关于HTML(1)认识HTML
  • 智能交通系统如何利用大数据、云计算和物联网技术优化交通流量、减少拥堵|智能交通系统|大数据|云计算|物联网|交通流量优化|减少拥堵
  • 记录一个前端学习小组的收集的模版
  • 在VB.net中,如何把20240906转化成日期格式
  • SSL和HTTPS是一样的吗?
  • 解决ruoyi框架中使用pagehelper插件分页查询后对数据进行对象转换后失效问题