js实现无限滚动 滚动到底部加载新数据

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
    <title></title>
	//<link rel="stylesheet" href="css/bootstrap.min.css">
    //<link rel="stylesheet" href="sui-mobile/dist/css/sm.min.css">
	//<link rel="stylesheet" href="sui-mobile/dist/css/sm-extend.min.css">
	<script src="sui-mobile/_site/assets/js/jquery.min.js"></script>
</head>
<body style="background-color: #fff;margin: 0; overflow: auto;">
	<header class="bar bar-nav">
	          <h1 class="title">底部无限滚动</h1>
	      </header>
				<table id="xztb" class="table" style="margin-top: 100px;">
					<tr style="background-color: #f1f1f1;border-bottom: 1px solid #f1f1f1;">
						<th style="white-space: nowrap">品名</th>
						<th style="white-space: nowrap">库存</th>
						
					</tr>
					<tbody id="showtable" >
					<!-- <tr><td><div class="no">暂无数据</div></td></tr> -->
					</tbody>
				</table>
				<div class="infinite-scroll-preloader">
				    <div class="preloader"></div>
				</div>
				
	</body>
	//<script src='sui-mobile/dist/js/sm.js'></script>
	<script type="text/javascript">
		
		$(".infinite-scroll-preloader").hide()
		
		// 最多加载的条数
		      var maxItems = 100;
		
		      // 每次加载添加多少条目
		      var itemsPerLoad = 20;
			  
			  
			  function add(number,lastIndex){
				  console.log(number)
				  console.log(lastIndex)
				  var html = '';
				                for (var i = lastIndex + 1; i <= lastIndex + number; i++) {
				                    html += '<tr class="len"><td >'+i+'</td></tr>';
				                }
				                // 添加新条目
				                $('#showtable').append(html);
			  }
			  add(itemsPerLoad,0)
			        // 上次加载的序号
			        var lastIndex = 20;
		window.onscroll = function() {        
			
			//变量scrollTop是滚动条滚动时,距离顶部的距离        
			var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;        
			//变量windowHeight是可视区的高度        
			var windowHeight = document.documentElement.clientHeight || document.body.clientHeight;        
			//变量scrollHeight是滚动条的总高度        
			var scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight;        
			//滚动条到底部的条件        
			if (scrollHeight - (scrollTop + windowHeight) < 1) { 
				
				if (lastIndex >= maxItems) {
					console.log("没数了")
				    return false;
				}
				//写后台加载数据的函数          
				// console.log("距顶部" + scrollTop + "可视区高度" + windowHeight + "滚动条总高度" + scrollHeight);   
					 $(".infinite-scroll-preloader").show()
					 console.log("出现转圈")
					 add(itemsPerLoad,lastIndex)
					 lastIndex = $('.len').length;
					 $(".infinite-scroll-preloader").hide()
					 console.log("关闭转圈")
					 
					 
				}    
			
		}
		
	</script>
	<style type="text/css">
	      .infinite-scroll-preloader {
	        margin-top:-20px;
	      }
	</style>
	
</html>

https://sui.thinkadmin.top/
我用的这个组件库 需要可以去看看

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
参数说明 listLoadingMore({id:"",id2:function(){},action:function(){},pageNum:10,getNew:undefined,funcArg:undefined,func:function(){},loadingMustTime:0,loadingDom:function(){},loadedDom:function(){},nullDataFunc:function(){},endFunc:function(){},errorFunc:function(){}}); id:滚动条id,不可以是body; id2:滚动列表的id,通过function自根据情况返回; action:数据来源的ajax地址,插件会自动加上page和length(分别表示请求页数和当前已有记录数),返回格式统一是[{},{}...],此类jsonArray; ajaxType:ajax提交方式,将更改传递数据的方式,默认post; pageNum:每页加载数量,将会根据这个数量判断是否全部加载完成; getNew:自定义获取数据方法; funcArg:自定义获取数据时传递的参数,类型为函数,返回所需参数; childrenTag:列表子项的标签,默认LI; func:非自定义获取数据时,创建每数据的方法; loadingMustTime:强制最小加载时间,默认0; loadingDom:加载中动画自定义,返回Dom或者HTML代码; loadedDom:完全加载完成动画自定义,返回Dom或者HTML代码; nullDataFunc:数据列表为空时执的方法; endFunc:每页数据加载完成时执的方法; errorFunc:ajax获取数据失败时调用的方法。 回调方法 调用本方法初始化后将会得到一个专属的方法集: Object {getNew:function,isFull:function,isRunning:function,isStop:function,loading:function,show:function}; getNew():忽略判断条件,强制执加载操作。此时数据传递中,page值可能出错,但length值一定正确,请谨慎判断后在执该方法; isFull(boolean):修改列表加载完成标志。true为全部加载完成,false则相反; isRunning(boolean):修改列表当前是否正在加载中标志。几乎没有使用的机会; isStop(boolean):设置是否停止加载,设置为true时将会阻止未来的所有加载操作; loading():根据当前滚动条的位置,自动判断是否需要加载数据; show(boolean,boolean):强制设置加载中和加载完成dom的显示或隐藏,true表示显示,false相对。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值