console
const ele = document.getElementById("ele")
if(isInviewport(ele)){
console.log("在屏幕可视区域")
}else{
console.log("不在屏幕可视区域")
}
function isInviewport(ele){
const rect = ele.getBoundingClientRect()
return (rect.top>=0 && rect.left>=0 &&
rect.bottom<=(document.documentElement.clientHeight) && rect.right<=(document.documentElement.clientWidth)
)}
const io = new IntersectionObserver((entries)=>{
if(entries[0].intersectionRatio<=0){
return console.log("entries","试图不可见")
}else{
return console.log("试图可见")
}
})
io.observe(document.getElementById("isScoll"))
<div class="box" id="ele"></div>
<div id="isScoll"></div>
.box{
width:500px;
height: 440px;
background: red
}
#isScoll{
width:500px;
height: 300px;
background: yellow
}