下拉加载下一页

本文介绍了一种利用Ajax技术实现网页滚动时自动加载更多数据的方法,通过监听滚动事件,判断页面是否接近底部,进而发起Ajax请求获取额外的数据,并更新页面内容,提升用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<script>
var page=2;
var finished=0;
$(document).ready(function(){
	var range = 1; //距下边界长度/单位px
	var pages = $("#listPages").val();
	var totalheight = 0;
	var main = $("#loadList"); //主体元素
	$(window).scroll(function(){
		var srollPos = $(window).scrollTop(); //滚动条距顶部距离(页面超出窗口的高度)
		totalheight = parseFloat($(window).height()) + parseFloat(srollPos);
		if(finished == 0){
			if(($(document).height()-range) < totalheight) {
				finished=1;
				if(page < pages || page == pages){
					nextPage(page);	
					page++;
				}else{
					$('#loading').remove();
					$('#loadList').append('<a href="javascript:;" class="clear loadMoreBtn">Duang~到底了</a>');
				}
			}
		}
	});
	
});

function nextPage(page)
{
	$.ajax({
         url:'{url:/college/college_newlist_ajax}',
         type:'POST',
         data:"num="+page+"&top=1",
         dataType:'json',
         success:function(json){
			 if(json.error_code > 0)
			 {
				alert(json.error_msg);
				return;
			 }
			 if(typeof json == 'object'){
				var commentHtml = '';
				for(var item in json.data)
				{
					if(json.data[item]['attribute'] == 'v'){
						commentHtml += template.render('videoTemplate',json.data[item]);;
					}else if(json.data[item]['thumb_img_number'] > 1){
						commentHtml += template.render('moreTemplate',json.data[item]);
					}else{
						commentHtml += template.render('oneTemplate',json.data[item]);
					}
					
				}
				commentHtml+='<a id="loading" href="javascript:;" class="clear loadMoreBtn">加载中...</a>';
				$('#loadList').append(commentHtml);
				$('#loading').remove();
				finished=0;
			 }
			
         }
     });

}
</script>

  

转载于:https://www.cnblogs.com/gengting/p/5508470.html

在Vue.js中实现下拉刷新加载更多内容通常涉及到两个核心组件:`v-loading`(用于显示加载状态)和`infinite-scroll`(用于处理滚动事件并触发数据的分页加载)。以下是简单的步骤: 1. 首先,在你的组件模板里引入这两个依赖库,例如使用Element UI提供的`el-loading`: ```html <template> <div ref="listContainer"> <!-- ... --> <div v-if="isLoading" class="loading">正在加载...</div> <ul infinite-scroll :trigger="loadMore" :distance="200"> <!-- 你的列表项 --> </ul> </div> </template> ``` 2. 定义必要的数据状态和方法: ```javascript <script> export default { data() { return { items: [], // 存放已加载的数据 isLoading: false, // 加载状态标志 page: 1, // 当前页面 perPage: 10, // 每页加载的数量 }; }, methods: { loadMore() { if (!this.isLoading && this.page < MAX_PAGES) { // 判断是否还有未加载的页面 this.isLoading = true; // 设置加载状态 // 发起网络请求加载一页数据 axios.get('/api/data?page=' + (this.page + 1)) .then(response => { this.items.concat(response.data); // 将新数据追加到现有列表 this.page++; // 更新当前页码 this.isLoading = false; // 数据加载完成,设置加载状态为false }) .catch(error => { console.error('Error loading more data:', error); }); } }, }, }; </script> ``` 3. 上述代码中,当用户滚动到底部时(通过`infinite-scroll`监听),会触发`loadMore`方法。这个方法会在每次调用时发送一个GET请求,参数包含当前页数,然后将返回的新数据合并到现有的`items`数组。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值