Older IE and Firefox browsers attach a scrollbar to documentElement , or the <html> in HTML.
All other browsers attach a scrollbar to document.body , or the <body> in HTML.
The correct solution would be to check which one to use, depending on the browser
var doc = document.documentElement.clientHeight ? document.documentElement : document.body; var s = $(doc).scrollTop();
jQuery makes this a little easier when passing to a window or document jQuery scrollTop does a similar check and computes it, so any of them should work in a cross browser
var s = $(document).scrollTop();
or
var s = $(window).scrollTop();
jQuery scrollTop () docs
Description: Get the current vertical position of the scroll bar for the first element in the set of matched elements, or set the vertical position of the scroll bar for each matched element.
... nothing works for my div, just a full page
If this is for a DIV, you will need to target an element with an attached scrollbar to get the scroll amount
$('div').scrollTop();
If you need to get the distance of elements from the top of the document, you can also do
$('div').offset().top
adeneo Feb 14 '13 at 23:39 2013-02-14 23:39
source share