LoginSignup
0
1

More than 1 year has passed since last update.

Intersection Observer API(監視をやめる)

Last updated at Posted at 2022-11-13

スクロールするたびに、監視してしまい、PCに負荷がかかる。
監視をやめるためには、、

{
  const target = document.querySelector('img');

  function callback(entries, obs) {
    console.log(entries[0]);

    if (!entries[0].isIntersecting) {
      return;
    }

    entries[0].target.classList.add('appear');
    obs.unobserve(entries[0].target);
  }

  const options = {
    threshold: 0.2,
  };

  const observer = new IntersectionObserver(callback, options);

  observer.observe(target);

・引数をもう一つ渡す。
・unobserve()を使い止める。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1