干货
元素出现在可视区
scorll滑动的距离>=当前元素距离浏览器最顶端的高度+当前元素自身的高度-当前可视区的高度
触底
scorll滑动的距离>=当前scroll总高度-当前可视区的高度
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
*{
padding: 0;
margin: 0;
box-sizing: border-box;
}
.a{
width: 100%;
/*height: 100%;*/
box-sizing: border-box;
/*overflow: auto;*/
}
.b{
width: 100px;
height: 100px;
border: 1px solid #999;
background: #f40;
margin-top: 2000px;
}
</style>
</head>
<body>
<div class="a" >
<div class="b">
</div>
</div>
</body>
<script type="text/javascript">
let a=document.getElementsByClassName('a')
let b=document.getElementsByClassName('b')
window.onscroll=function(){
console.log('scroll总高度'+document.documentElement.scrollHeight)
console.log("滚动条的滚动的高度"+ document.documentElement.scrollTop)
console.log("元素距离浏览器的高度"+b[0].offsetTop)
console.log("可视区"+document.documentElement.clientHeight)
console.log('整体的高度'+(Number(document.documentElement.scrollTop)+Number(document.documentElement.clientHeight)))
}
</script>
</html>