Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

169
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda