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

271
Views
How can i add new elements at the beggining while i am scrolling?

I have this code where when i am scrolling down i am appending new elements

<div class="card-container">
        <div class="card show">First card</div>
        <div class="card">card</div>
        <div class="card">card</div>
        <div class="card">card</div>
        <div class="card">card</div>
        <div class="card">card</div>
        <div class="card">card</div>
        <div class="card">card</div>
        <div class="card">card</div>
</div>

JS

const cards = document.querySelectorAll('.card');
const observer = new IntersectionObserver(entries => {
    entries.forEach(entry => {
        entry.target.classList.toggle('show', entry.isIntersecting);
    })
})

const lastCardObserver = new IntersectionObserver(entries => {
    const lastCard = entries[0];
    if(!lastCard.isIntersecting) return
    loadNewCards();
    lastCardObserver.unobserve(lastCard.target);
    lastCardObserver.observe(document.querySelector('.card:last-child'));
}, {rootMargin: '200px'})

const cardContainer = document.querySelector('.card-container');
function loadNewCards() {
    for(let i = 0;i < 10;i++) {
        const card = document.createElement('div');
        card.textContent = 'new card';
        card.classList.add('card');
        card.classList.add(`card-${i}`);
        observer.observe(card);
        cardContainer.append(card);
    }
}

cards.forEach(card => {
    observer.observe(card);
})

lastCardObserver.observe(document.querySelector('.card:last-child'))

EVERYTHING WORKS FINE

but i need now when i am at the beggining at the element which is on the viewport, while i scroll up to add new elements so to detect that i am scrolled to the first element in the viewport

How can i do that

i tried

this.firstCardObserver = new IntersectionObserver(entries => {
      const firstCard = entries[0];
      if (!firstCard.isIntersecting) return;
      this.loadBefore();
    }, { rootMargin: '200px' , threshold: 0.5 })
    this.firstCardObserver.observe(document.querySelector('.card:first-child'))

but it does notr seems to work. I get always new data when i come initially on my page

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!