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
jQuery 'scroll' not firing

I have the following:

 var onScroll = function () {
        alert('scroll');
 };
 scrollArea = $('body');
 scrollArea.on('scroll', onScroll);

I can do $('body').scrollTop(400) and the body scrolls fine, but the alert is never called when I manually scroll. What could be causing this?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

When you scroll the page, it's window that is scrolling not the body. So change it to $(window).on('scroll', onScroll);

EDIT: If you want to bind it to body specifically you may do it using document.body.onscroll = onScroll;. But make sure you do not bind any onscroll event to window in that case. If you do so, document.body.onscroll will be ignored and window.onscroll will be listened to.

Check this in action at the fiddle link here.

over 4 years ago · Santiago Trujillo Report

0

Don't use $('body'), but either $(document) or $(window) to catch the scroll, like this:

$(document).ready(function() {
    $(document).scroll(function() {
    console.log('Scrolled to ' + $(this).scrollTop());
  })
});

Here's a working jsFiddle: https://jsfiddle.net/4rrm3myL/1/

over 4 years ago · Santiago Trujillo Report
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!