質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

Q&A

解決済

1回答

951閲覧

JavaScript IntersectionObserverで要素が画面内に入ってきたかを判定する

zdww

総合スコア2

JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

0グッド

0クリップ

投稿2021/06/16 03:31

編集2021/06/16 03:34

IntersectionObserverについてです
要素が画面内に入ってきたかを判定するもので、
画面内に入っていたらstyle.background="red"としたいですが、
最初の実行で画面に入ってるdivタグ10個に対し、赤色にし、以後スクロールに応じて個別に赤色にしていきたいですが、

最初の実行で画面に入ってるdivタグ10個に対し、赤色がうまくいきません

現状のコードは下記になります
どのようにすれば実現可能でしょうか?

疑問点としてスクロールした際に取りこぼしのような事象も発生しています
スクロールに応じて赤くなっていく際に1個飛ばしで赤くなったりという点です
詳しい方教えて下さい

<style> body,html{ height:100%; } </style> <script> let options = { root:document.querySelector("#test"), threshold:0, rootMargin:"0px 0px -100% 0px" } window.onload=function(){ const observer = new IntersectionObserver((entries) => { if(entries[0].isIntersecting){ document.getElementById(entries[0].target.id).style.background="red"; } }, options); a=document.querySelectorAll('.divdiv'); c=a.length-1; for(i=0;i<=c;i++){ observer.observe(a[i]); } } </script> <div id="test"> <div id="aaaa1" class="divdiv" style="width:100%;height:10%">div</div> <div id="aaaa2" class="divdiv" style="width:100%;height:10%">div</div> <div id="aaaa3" class="divdiv" style="width:100%;height:10%">div</div> <div id="aaaa4" class="divdiv" style="width:100%;height:10%">div</div> <div id="aaaa5" class="divdiv" style="width:100%;height:10%">div</div> <div id="aaaa6" class="divdiv" style="width:100%;height:10%">div</div> <div id="aaaa7" class="divdiv" style="width:100%;height:10%">div</div> <div id="aaaa8" class="divdiv" style="width:100%;height:10%">div</div> <div id="aaaa9" class="divdiv" style="width:100%;height:10%">div</div> <div id="aaaa10" class="divdiv" style="width:100%;height:10%">div</div> <div id="aaaa11" class="divdiv" style="width:100%;height:10%">div</div> <div id="aaaa12" class="divdiv" style="width:100%;height:10%">div</div> <div id="aaaa13" class="divdiv" style="width:100%;height:10%">div</div> <div id="aaaa14" class="divdiv" style="width:100%;height:10%">div</div> <div id="aaaa15" class="divdiv" style="width:100%;height:10%">div</div> <div id="aaaa16" class="divdiv" style="width:100%;height:10%">div</div> <div id="aaaa17" class="divdiv" style="width:100%;height:10%">div</div> <div id="aaaa18" class="divdiv" style="width:100%;height:10%">div</div> <div id="aaaa19" class="divdiv" style="width:100%;height:10%">div</div> <div id="aaaa20" class="divdiv" style="width:100%;height:10%">div</div> <div id="aaaa21" class="divdiv" style="width:100%;height:10%">div</div> <div id="aaaa22" class="divdiv" style="width:100%;height:10%">div</div> <div id="aaaa23" class="divdiv" style="width:100%;height:10%">div</div> <div id="aaaa24" class="divdiv" style="width:100%;height:10%">div</div> <div id="aaaa25" class="divdiv" style="width:100%;height:10%">div</div>

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

要素が画面内に入ってきたかを判定する

現状だと #test との交差を判定しているので、オプションの指定は

javascript

1let options = { 2 threshold: 0, 3}

でいいと思います。

また、なぜか entries の最初の要素しか処理の対象になっていないので、きちんとループして全ての対象に対して処理を行ってください。

javascript

1const observer = new IntersectionObserver((entries) => { 2 for (const entry of entries) { 3 if (entry.isIntersecting) { 4 entry.target.style.background = "red"; 5 } 6 } 7}, options);

isIntersectingIntersectionObserverEntry のプロパティとなりますので、判定する場合 entry.isIntersecting を使用してください。

  • entry : IntersectionObserverEntry
  • entry.target : Element
  • entry.target.id : DOMString

投稿2021/06/16 06:34

編集2021/06/16 07:10
退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

zdww

2021/06/16 06:44

教えていただいたソースですと一瞬で全て赤になります 初回ロード時は全部赤でいいですが、スクロールした場合1個づつ赤にしていきたいのですが <style> body,html{ height:100%; } </style> <script> let options = { root:document.querySelector("#test"), threshold:0 } window.onload=function(){ const observer = new IntersectionObserver((entries) => { for (const entry of entries) { entry.target.style.background = "red"; } }, options); a=document.querySelectorAll('.divdiv'); c=a.length-1; for(i=0;i<=c;i++){ observer.observe(a[i]); } } </script> <div id="test" style="background:yellow;overflow-x: scroll;width:100%;height:100%"> <div id="aaaa1" class="divdiv" style="width:100%;height:10%">div</div> <div id="aaaa2" class="divdiv" style="width:100%;height:10%">div</div> <div id="aaaa3" class="divdiv" style="width:100%;height:10%">div</div> <div id="aaaa4" class="divdiv" style="width:100%;height:10%">div</div> <div id="aaaa5" class="divdiv" style="width:100%;height:10%">div</div> <div id="aaaa6" class="divdiv" style="width:100%;height:10%">div</div> <div id="aaaa7" class="divdiv" style="width:100%;height:10%">div</div> <div id="aaaa8" class="divdiv" style="width:100%;height:10%">div</div> <div id="aaaa9" class="divdiv" style="width:100%;height:10%">div</div> <div id="aaaa10" class="divdiv" style="width:100%;height:10%">div</div> <div id="aaaa11" class="divdiv" style="width:100%;height:10%">div</div> <div id="aaaa12" class="divdiv" style="width:100%;height:10%">div</div> <div id="aaaa13" class="divdiv" style="width:100%;height:10%">div</div> <div id="aaaa14" class="divdiv" style="width:100%;height:10%">div</div> <div id="aaaa15" class="divdiv" style="width:100%;height:10%">div</div> <div id="aaaa16" class="divdiv" style="width:100%;height:10%">div</div> <div id="aaaa17" class="divdiv" style="width:100%;height:10%">div</div> <div id="aaaa18" class="divdiv" style="width:100%;height:10%">div</div> <div id="aaaa19" class="divdiv" style="width:100%;height:10%">div</div> <div id="aaaa20" class="divdiv" style="width:100%;height:10%">div</div> <div id="aaaa21" class="divdiv" style="width:100%;height:10%">div</div> <div id="aaaa22" class="divdiv" style="width:100%;height:10%">div</div> <div id="aaaa23" class="divdiv" style="width:100%;height:10%">div</div> <div id="aaaa24" class="divdiv" style="width:100%;height:10%">div</div> <div id="aaaa25" class="divdiv" style="width:100%;height:10%">div</div></div>
退会済みユーザー

退会済みユーザー

2021/06/16 06:51

ループ内で `isIntersecting` の判定をしてください。
zdww

2021/06/16 06:56

こういうことでしょうか? 動きませんでした for (const entry of entries) { if(entry.target.isIntersecting){ entry.target.style.background = "red"; } }
zdww

2021/06/16 07:02

やはり動きません const observer = new IntersectionObserver((entries) => { for (const entry of entries) { if(entry.target.id.isIntersecting){ document.getElementById(entry.target.id).style.background = "red"; } }
退会済みユーザー

退会済みユーザー

2021/06/16 07:12

回答追記しましたので参考にしてください。
zdww

2021/06/16 09:26

助かりました。ありがとうございました。 あと observer.observe(a[i]); で追加されたものを個別に削除するにはどうしたらいいかご存知でしょうか?
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問