1 回答
TA贡献1860条经验 获得超8个赞
entry.isIntersecting直接用于classList.toggle()第二个(布尔)参数。
使用 options {threshold: 0},或者根本不使用,因为阈值默认为0
const check = (entries) => entries.forEach(entry => {
entry.target.classList.toggle("is-active", entry.isIntersecting);
});
const Obs = new IntersectionObserver(check);
document.querySelectorAll("section").forEach(el => Obs.observe(el));
* {margin:0; box-sizing: border-box;}
section {
min-height: 100vh;
transition: 0.5s;
transform: scale(0.4);
display: flex;
justify-content: center;
align-items: center;
font-size: 70vh;
}
.is-active {
transform: scale(1);
}
<section style="background: #0bf;">1</section>
<section style="background: #f0b;">2</section>
<section style="background: #fb0;">3</section>
<section style="background: #0fb;">4</section>
添加回答
举报