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

196
Views
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
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!