0
1

More than 1 year has passed since last update.

Intersection Observer API(範囲の指定応用)

Last updated at Posted at 2022-11-13

Intersection Observer API(範囲の応用)

thresholdを書いているところに追加する。
種類は大きく分けて2種類
1.root:null
→nullがデフォルト。細かく指定できる。%の
2.rootmargin:'0 0 0'
→marginをつける時と同じようにして書く。

ex)

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

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

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

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

  const options = {
    threshold: 1,
    rootMargin: '0px 0px -100px',
  };

  const observer = new IntersectionObserver(callback, options);

  observer.observe(target);
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