I am attempting to target a parent element and a child element using an intersectionObserver, then I have a function changing the background of the parent to a different color and rotating the child element.
This code works on the parent div, however the child item returns as undefined. Am I unable to target child elements with querySelector, or is the intersectionObserver unable to observe more than one element?
JavaScript
x
22
22
1
let options = {
2
threshold: 0.25
3
}
4
5
let observer = new IntersectionObserver(function(entries, observer) {
6
entries.forEach(entry => {
7
if (!entry.isIntersecting) {
8
return;
9
} else {
10
console.log(entry.target);
11
console.log(entry.sticky);
12
alert('INTERSECTING!');
13
entry.target.classList.toggle("red");
14
entry.sticky.classList.toggle("rotate");
15
}
16
});
17
}, options);
18
19
let target = document.querySelector('.placeholder__div__large');
20
let sticky = document.querySelector('.sticky__container');
21
22
observer.observe(target, sticky);
JavaScript
1
37
37
1
.placeholder__div__large {
2
height: 200vh;
3
width: 100vw;
4
display: flex;
5
align-items: center;
6
justify-content: center;
7
color: white;
8
background: black;
9
transition: 2s;
10
}
11
12
.sticky__container {
13
position: sticky;
14
top: 100px;
15
width: 200px;
16
height: 200px;
17
}
18
19
.sticky__item {
20
display: flex;
21
justify-content: center;
22
align-items: center;
23
text-align: center;
24
background: white;
25
color: black;
26
width: 100%;
27
height: 100%;
28
}
29
30
.red {
31
background: red;
32
transition: 2s;
33
}
34
35
.rotate {
36
transform: rotate(180deg);
37
}
JavaScript
1
5
1
<div class="placeholder__div__large">
2
<div class="sticky__container">
3
<div class="sticky__item">STICKY ITEM</div>
4
</div>
5
</div>
Advertisement
Answer
You can’t observe multiple elements by passing them all to .observe
, you have to call it multiple times.
Also, I assume you rather wanted to do it like this (I’m not sure if I’m right, but parts of your code didn’t make any sense to me):
JavaScript
1
16
16
1
let options = {
2
threshold: 0.25
3
}
4
5
const observer = new IntersectionObserver(function(entries, observer) {
6
entries.forEach(entry => {
7
console.log('INTERSECTING with', entry.target, entry.isIntersecting);
8
entry.target.classList.toggle("intersect", entry.isIntersecting);
9
});
10
}, options);
11
12
const target = document.querySelector('.placeholder__div__large');
13
const sticky = document.querySelector('.sticky__container');
14
15
observer.observe(target);
16
observer.observe(sticky);
JavaScript
1
37
37
1
.placeholder__div__large {
2
height: 200vh;
3
width: 100vw;
4
display: flex;
5
align-items: center;
6
justify-content: center;
7
color: white;
8
background: black;
9
transition: 2s;
10
}
11
12
.sticky__container {
13
position: sticky;
14
top: 100px;
15
width: 200px;
16
height: 200px;
17
}
18
19
.sticky__item {
20
display: flex;
21
justify-content: center;
22
align-items: center;
23
text-align: center;
24
background: white;
25
color: black;
26
width: 100%;
27
height: 100%;
28
}
29
30
.red-whenintersect.intersect {
31
background: red;
32
transition: 2s;
33
}
34
35
.rotate-whenintersect.intersect {
36
transform: rotate(180deg);
37
}
JavaScript
1
5
1
<div class="placeholder__div__large red-whenintersect">
2
<div class="sticky__container">
3
<div class="sticky__item rotate-whenintersect">STICKY ITEM</div>
4
</div>
5
</div>