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

206
Views
How to make window.onscroll or window.addEventListener('scroll') events to fire

I am trying to listen to window.onscroll event to enable me do lazy loading of content in an app I am building primarily using tailwind CSS and Alpine.js, so far I have this html:

<div class="flex-1 flex flex-col" :style="`height: ${window.innerHeight}px;`" data-content>
    <div class="flex-1 flex items-stretch h-inherit overflow-y-auto">
        <main class="flex-1">
            <section aria-labelledby="primary-heading" class="min-w-0 flex-1 h-full flex flex-col lg:order-last">
                <div class="block h-full w-full text-gray-200">
                    <!-- Main content Goes here -->
                </div>
            </section>
        </main>
        <aside class="sticky top-0 hidden w-80 bg-white border-l border-gray-200 overflow-y-auto lg:block">
            <div class="p-6 h-full">
                <div class="block border-2 border-dashed border-gray-300 rounded h-full w-full text-gray-200">
                    <!-- My Secondary navigation goes here-->
                </div>
            </div>
        </aside>
    </div>
</div>

Now the problem is, since my content is restricted to the height of the window, no onscroll events are being registered when i run:

window.onscroll = e => {
    console.log(e)
}

I have also tried:

document.querySelector('['data-content']').onscroll = e => {
    console.log(e)
}

In this case, the onscroll event is registered and this brings us to a second problem. This time around, when I try to the following code which is supposed to let me know when the user has reached the end of the page so that I can fetch more content, scrollTop + clientHeight and scrollHeight remains the same from the beginning of the scroll to the end.

document.querySelector('['data-content']').onscroll = e => {
    const { scrollTop, scrollHeight, clientHeight } = document.documentElement;
    console.log(scrollTop + clientHeight, scrollHeight);
};

I would realy appreciate any help or recommendations as I seem to have been stuck here for quite a while now.

EDIT The result is the same even when I use window.addEventListener('scroll')

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!