我正在尝试获取部分在视口中的位置并向它们添加活动类,但代码不起作用。任何人都可以帮忙吗?
// Get the position of the sections in viewport
function isInViewport(element) {
const rect = element.getBoundingClientRect();
return (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
rect.right <= (window.innerWidth || document.documentElement.clientWidth)
);
}
//add active class to sections in viewport
function setActiveClass(){
for (let i=0; i <= sectionsArr.length; i++){
if (isInViewport(sections[i])){
sections[i].classList.add("your-active-class");
}else{
sections[i].classList.remove("your-active-class");
}
}
}
MMTTMM
相关分类