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

173
Views
intersectionObserver API: If the element is outside of the viewport

I wrote the bellow code which is adding some inline styling to an element once the element is out of the viewport.

const myDiv = document.querySelector('.my-div')

const obr = new IntersectionObserver(function(entries) {
    if (entries[0].intersectionRatio <= 0) {
    myDiv.style.top = '100px';
  }
});

obr.observe(myDiv);

The above code works fine.

But now I need to add the inline styling when only 100px of the element's height is still in the viewport. In other words the styling shouldn't get added once the element is entirely outside of the viewport but it should be added when most part of it is outside of the viewport and 100px of its height is still in the viewport.

How can I change the above code to achieve that? I think I need to set rootMargin to 100px, but not sure how to implement it into my code.

I tried this:

const observer = new IntersectionObserver({ rootMargin: '100px' }, entries => {...

But then I get the following error:

Uncaught TypeError: Failed to construct 'IntersectionObserver': parameter 1 is not of type 'Function'.
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!