Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

201
Views
Why does my intersection observer trigger the callback on load although target is not within threshold?

I am trying to manipulate a target object using the intersection observer API once 0.5 of it are within viewport which actually seems to work. However, the callback function also fires straight upon page load and it seems it also fires again once scrolling back up, why's that?

let options = {
  root: null, // set the root
  rootMargin: '0px',
  threshold: 0.5 // 1.0 means 100% of target has to be visible to trigger callback
};

// Declare the target element to manipulate
const target = document.querySelector('.target');

let callback = (entries, observer) => {
  entries.forEach(entry => {
    target.classList.toggle( 'target-hit' , entries[0].isIntersecting );
    console.log('fired')
  });
};

// Create intersection observer
let observer = new IntersectionObserver(callback, options);

// Initiate observer to watch for target
observer.observe( target );
.non-target { 
  height: 1000px 
}

.target { 
  background-color: red;
  width: 40px;
  height: 40px;
  margin-bottom: 30px;
}

.target-hit {
    background-color: green
}
<div class="non-target">Non-Target - Please scroll down to target :)</div>
<div class="target">Target</div>

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!