代码实现:
JS:
window.onscroll = function () {
//变量scrollTop是滚动条滚动时,距离顶部的距离,兼容ie
var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
//变量windowHeight是可视区的高度,兼容ie
var windowHeight = document.documentElement.clientHeight || document.body.clientHeight;
//变量scrollHeight是滚动条的总高度,兼容ie
var scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight;
//滚动条到底部的条件,兼容ie
if (scrollTop + windowHeight === scrollHeight) {
console.log('已经下拉到底部啦!')
console.log('界面的总高度为:'+scrollHeight+',可视窗口的高度为:'+windowHeight+',滚动条距离顶部的高度为:'+scrollTop)
}
}
HTML:
<!DOCTYPE html>
<html>
<head>
<style>
#div{
height: 2000px;
background-color: aqua;
}
</style>
</head>
<body>
<h2>我的第一张网页</h2>
<p>我的第一个段落。</p>
<p id="demo"></p>
<p name="demo"></p>
<div id='div'>div</div>
<script src="print-demo.js">
</script>
</body>
</html>
简单明了