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

198
Visualizações
Check if element is in viewport Vanilla JS

I want my element appear via keyframe animation if they are visible on current window viewport.

Its kinda half working but my classList.remove('current') overrides my classList.add('current') so the behavior while scrolling isnt that smooth.

My Code

CSS

section.section{
    &.current{
        animation:expand 1s ease-in-out;
    }
}

@keyframes expand {
    from {
        transform: scale(0);
        display: block;
    }
}

JS

const elements = document.querySelectorAll('section.section');
window.addEventListener('scroll', function (e) {
    elements.forEach(el => {
        let elTopPos = el.getBoundingClientRect().top  + window.scrollY;
        let elBottomPos = el.getBoundingClientRect().bottom  + window.scrollY;
        let windowBottomPos = window.innerHeight + window.pageYOffset;
        let windowTopPos = window.scrollY;
        if (el.classList.contains('current') === false) {
            if ((windowBottomPos > elTopPos && windowBottomPos < elBottomPos) || (windowTopPos < elBottomPos && windowTopPos > elTopPos)) {
                el.classList.add('current');
            }
        } else{
            if ((windowTopPos > elBottomPos && windowBottomPos > elBottomPos) || (windowBottomPos < elTopPos && windowTopPos < elTopPos)) {
                el.classList.remove('current');
            }
        }
    })
},{
    passive: true
})
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