用JavaScript实现上拉加载

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/csdnlinyongsheng/article/details/88036572
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
	<title>Document</title>
	<style>
		#wrap{
			height:1000px;
		}
	</style>
</head>
<body>
	<div id="wrap">
		上拉内容
		
	</div>
	<script>
		
		//获取滚动条当前的位置
		function getScrollTop() {
		    var scrollTop = 0;
		    if(document.documentElement && document.documentElement.scrollTop) {
		        scrollTop = document.documentElement.scrollTop;
		    } else if(document.body) {
		        scrollTop = document.body.scrollTop;
		    }
		    return scrollTop;
		}
		//获取当前可视范围的高度  
		function getClientHeight() {
		    var clientHeight = 0;
		    if(document.body.clientHeight && document.documentElement.clientHeight) {
		        clientHeight = Math.min(document.body.clientHeight, document.documentElement.clientHeight);
		    } else {
		        clientHeight = Math.max(document.body.clientHeight, document.documentElement.clientHeight);
		    }
		    return clientHeight;
		}
		//获取文档完整的高度 
		function getScrollHeight() {
		    return Math.max(document.body.scrollHeight, document.documentElement.scrollHeight);
		}

		window.onscroll = function() {
		    if(getScrollTop() + getClientHeight() == getScrollHeight()) {
		        console.log('上拉刷新了')
		        //此处发起AJAX请求
		    }
		}

	</script>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/csdnlinyongsheng/article/details/88036572
今日推荐