首页 > 解决方案 > Intersection Observer API addClass 不被观察

问题描述

使用 Intersection Observer API,是否可以classList.Add用于不是“观察到”的特定 div?

我想做的是:当 div.upup可见时, div.logoup将获得额外的 class .logonew

这是我的代码,但不幸的是我不知道如何定位.logoupdiv。

请帮忙 :)

  entry.forEach((change) => {
    if(change.isIntersecting) {
      change.target.classList.add('logonew');
    }
  });
}

let options = {
  threshold: [0.5]
};

let observer = new IntersectionObserver(onEntry, options);

let elements = document.querySelectorAll('.upup');

for (let elm of elements) {
  observer.observe(elm);
}

标签: apiclassaddintersection

解决方案


推荐阅读