在我们需要监听目标元素是否进入视口时,需要监听scroll事件,大量的计算会造成性能问题
IntersectionObserver API 是异步的,不随着目标元素的滚动同步触发。 即只有线程空闲下来,才会执行观察器。这意味着,这个观察器的优先级非常低,只在其他任务执行完,浏览器有了空闲才会执行。
直接放一个例子
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body{
margin: 0;
padding: 0;
height: 100vh;
width: 100vw;
overflow-x: hidden;
}
.flex{
display: flex;
}
.top-fixed{
top: 0;
position: fixed;
}
.placeholder1{
width: 100%;
}
#box1{
height: 200px;
overflow-y: auto;
border: 1px solid #aaa;
width: 60%;
/* margin-bottom:500px; */
}
.box1-placeholder{
height: 105vh;
}
#box2{
height: 100px;
background-color: blue;
margin-top: 300px;
width: 60%;
}
.box2-placeholder{
height: 205px;
}
.mb100{
height: 200px;
overflow-y: auto;
border: 1px solid #aaa;
width: 60%;
margin-bottom: 1300px;
}
</style>
</head>
<body>
<section class="flex top-fixed">
<div class="flex">BOX1:</div>
<div class="flex" id="box1-status">invisible</div>
<div class="flex"> BOX2:</div>
<div class="flex" id="box2-status">invisible</div>
</section>
<div class="box1-placeholder"></div>
<div id="box1" class="mb100">
<div class="box2-placeholder"></div>
<div id="box2"></div>
<div class="box2-placeholder"></div>
</div>
<div id="box1-1" class="mb100">
<div class="box2-placeholder"></div>
<div id="box2"></div>
<div class="box2-placeholder"></div>
</div>
<div id="box1-2" class="mb100">
<div class="box2-placeholder"></div>
<div id="box2"></div>
<div class="box2-placeholder"></div>
</div>
<div id="box1-3" class="mb100">
<div class="box2-placeholder"></div>
<div id="box2"></div>
<div class="box2-placeholder"></div>
</div>
<!-- <div id="box4">
<div class="box2-placeholder"></div>
<div id="box2"></div>
<div class="box2-placeholder"></div>
</div>
<div id="box5">
<div class="box2-placeholder"></div>
<div id="box2"></div>
<div class="box2-placeholder"></div>
</div> -->
<div class="box1-placeholder"></div>
</body>
<script>
(function(){
const box1 = document.querySelector("#box1");
const box2 = document.querySelector("#box2");
const box11 = document.querySelector("#box1-1");
const box12 = document.querySelector("#box1-2");
const box13 = document.querySelector("#box1-3");
const box1Status = document.querySelector("#box1-status");
const box2Status = document.querySelector("#box2-status");
const box1Observer = new IntersectionObserver(entries => {
entries.forEach(item => {
// `intersectionRatio`为目标元素的可见比例,大于`0`代表可见
console.log(item,'item');
if (item.intersectionRatio > 0) {
box1Status.innerText = "visible";
}else{
box1Status.innerText = "invisible";
}
});
}, {root: document});
const box2Observer = new IntersectionObserver(entries => {
entries.forEach(item => {
// `intersectionRatio`为目标元素的可见比例,大于`0`代表可见
if (item.intersectionRatio > 0) {
box2Status.innerText = "visible";
}else{
box2Status.innerText = "invisible";
}
});
}, {root: box1});
box1Observer.observe(box1);
box2Observer.observe(box2);
box1Observer.observe(box11);
box1Observer.observe(box12);
box1Observer.observe(box13);
})();
</script>
</html>