Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

166
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda