0

apparently .scrollTop() works only in webkit browsers... is this possible? that's very strange because i found some questions here in stackoverflow titled "scrollTop works only in Firefox" but what is happening to me it's different

$(window).scroll(function() {
    console.log($('body').scrollTop())
})

even if i replace window with document nothing changes. the funny thing is when i run this function and i scroll down the page the value still 0 BUT the red little badge beside the number 0 changes at every pixel scrolled...

In chrome and opera this works perfectly.

i'm running Firefox 34.0 on Win7x64 and i'm using jquery 2.1.3

2 Answers2

0

If you need scrolltop position, i prefer use offset:

$(window).scroll(function() {console.log($('body').offset().top) })
vsync
  • 87,559
  • 45
  • 247
  • 317
0

This is because WebKit sets the scrollTop for the main document on the body, while other browser use the html element. However, you can just use window instead of 'body' or 'html' to get the main document scroll position.

$(window).scroll(function() {
    console.log($(window).scrollTop())
});

BTW, calling jQuery on an object isn't the fasting thing in the world, and scroll events can fire very rapidly. Consider caching $(window) in a variable for improved performance.

var $window = $(window);
$window.scroll(function() {
    console.log($window.scrollTop())
});
Alexander O'Mara
  • 52,993
  • 16
  • 139
  • 151