0
0

intersectionObserverについて調べています。

Posted at

お疲れ様です。

再びポートフォリオの編集を行っているのですが、タイトルにある
intersectionObserverを使い、監視したい範囲を設定し、交差(特定の要素が監視の範囲に入った状態のこと)した時に、指定したアニメーションが再生されるようにしているのですが、再生できずに泣いています:innocent:

/監視対象が現れたらすること
const History = (entries) => {
  const move = {
    color: ['transparent', '#fff'],
    backgroundPosition: ['100% 0', '0 0'],
  };

  const choice = {
    duration: 1000,
    easing: 'ease',
  };
  entries.forEach(entry => {
    if(entry.isIntersecting){
      entry.target.animate(move, 3000);
    }
  });
};


// 監視設定
const history = new IntersectionObserver(History);

history.observe(document.querySelector('#history'));

始めはUncaught Type error が出ていたので調べると、 entries は配列扱いになるため、 entaries[0] と入力する必要があり、それでなんとかエラー解消できましたが、アニメは動きませんでした…:sob:

ですがエラーとか日々なんぼでも出会ってるので、へこまずコツコツやっていきたいと思います!

初心者程度の知識しか持ち合わせていませんので、さっそくchatGPT先生にコードを貼り付け、質問したところ、このコードで動かなかったら、 intersectionObserver のオプション指定をしていきましょう とのことでしたので、現在 intersectionObserver.rootrootMargin なるものについて、MDNのページで勉強中です。

交差について細かく指定できるプロパティなのでしょう。
とにかく記事を読んで、chatGPT先生が言っていることを理解できるようになるのを目指します:sweat_drops:

0
0
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
0