質問するログイン新規登録

回答編集履歴

1

コメントを受けて追記

2021/04/06 04:09

投稿

Lhankor_Mhy
Lhankor_Mhy

スコア37773

answer CHANGED
@@ -13,4 +13,38 @@
13
13
 
14
14
  >__threshold:1にしてるにも関わらず初回ロード時にs1が表示されるのもおかしいです__
15
15
 
16
- IntersectionObserverに登録された時に発火する仕様です。
16
+ IntersectionObserverに登録された時に発火する仕様です。
17
+
18
+ ---
19
+
20
+ # コメントを受けて追記
21
+
22
+ s2が発火するかどうかについては、ユーザーの操作次第になりますが。
23
+
24
+ ```js
25
+ window.onload = function () {
26
+
27
+ // document.querySelector("#aaa")が取得できないので移動
28
+ let options = {
29
+ root: document.querySelector("#aaa"),
30
+ threshold: 1
31
+ }
32
+
33
+ const observer = new IntersectionObserver((entries) => {
34
+ if (entries[0].intersectionRatio != 1) { // 条件式変更
35
+ return;
36
+ } else {
37
+ alert(entries[0].target.id);
38
+ }
39
+ }, options);
40
+
41
+ // 1pxだけスクロールする(ロード時対策)
42
+ observer.root.scrollTo(0, 1);
43
+
44
+ s = document.querySelectorAll('.section');
45
+ sl = s.length;
46
+ for (i = 0; i < sl; i++) {
47
+ observer.observe(s[i]);
48
+ }
49
+ }
50
+ ```