Why is my intersectionObserver returning my querySelect as undefined?

kyija

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?

let options = {
  threshold: 0.25
}

let observer = new IntersectionObserver(function(entries, observer) {
  entries.forEach(entry => {
    if (!entry.isIntersecting) {
      return;
    } else {
      console.log(entry.target);
      console.log(entry.sticky);
      alert('INTERSECTING!');
      entry.target.classList.toggle("red");
      entry.sticky.classList.toggle("rotate");
    }
  });
}, options);

let target = document.querySelector('.placeholder__div__large');
let sticky = document.querySelector('.sticky__container');

observer.observe(target, sticky);
.placeholder__div__large {
  height: 200vh;
  width: 100vw;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  background: black;
  transition: 2s;
}

.sticky__container {
  position: sticky;
  top: 100px;
  width: 200px;
  height: 200px;
}

.sticky__item {
  display: flex;
  justify-content: center;
  align-items: center;
  text-align: center;
  background: white;
  color: black;
  width: 100%;
  height: 100%;
}

.red {
  background: red;
  transition: 2s;
}

.rotate {
  transform: rotate(180deg);
}
<div class="placeholder__div__large">
  <div class="sticky__container">
    <div class="sticky__item">STICKY ITEM</div>
  </div>
</div>

FZs

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):

let options = {
  threshold: 0.25
}

const observer = new IntersectionObserver(function(entries, observer) {
  entries.forEach(entry => {
    console.log('INTERSECTING with', entry.target, entry.isIntersecting);
    entry.target.classList.toggle("intersect", entry.isIntersecting);
  });
}, options);

const target = document.querySelector('.placeholder__div__large');
const sticky = document.querySelector('.sticky__container');

observer.observe(target);
observer.observe(sticky);
.placeholder__div__large {
  height: 200vh;
  width: 100vw;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  background: black;
  transition: 2s;
}

.sticky__container {
  position: sticky;
  top: 100px;
  width: 200px;
  height: 200px;
}

.sticky__item {
  display: flex;
  justify-content: center;
  align-items: center;
  text-align: center;
  background: white;
  color: black;
  width: 100%;
  height: 100%;
}

.red-whenintersect.intersect {
  background: red;
  transition: 2s;
}

.rotate-whenintersect.intersect {
  transform: rotate(180deg);
}
<div class="placeholder__div__large red-whenintersect">
  <div class="sticky__container">
    <div class="sticky__item rotate-whenintersect">STICKY ITEM</div>
  </div>
</div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

why is my object returning undefined?

Why is my JavaScript hoisted local variable returning undefined but the hoisted global variable is returning blank?

Why is my promise returning undefined?

Why is my function is undefined?

Why is my get request returning undefined in my image file tree?

why is my gestureRecognizer returning nil?

Why is my variable returning undefined in javascript?

Why is my reducer returning undefined, React TypeScript

Why is my recursive function returning undefined?

Why is my variable tabledata returning undefined?

Why is my array returning null?

Why is my inner function returning as undefined in my simple Javascript OOP game

Why is my function returning undefined along with the reversed string

Why is my code returning my else: statement?

Why is my method returning infinity?

Why is my program returning "0.0"?

my variable is returning empty why?

Why is my email check variable returning an error as undefined

Why is my function returning an array with undefined as items?

Why is my method not returning anything?

why does my functions returning "undefined"

Why is my for loop not returning anything?

Why is my axios post returning undefined in my functional component?

why does my nodejs function returning undefined?

Why is my function returning undefined despite printing out correctly

Why is my RegExp.prototype.exec() returning array of undefined?

why my Nodejs / NestJs promise is returning undefined to variable in second function?

Why my intersectionObserver and css animation not working?

Why is my variable returning as 'undefined' despite being set a value?