views:

564

answers:

3

How can I find out what percentage of the vertical scrollbar a user has moved through at any given point?

It's easy enough to trap the 'onscroll' event to fire when the user scrolls down the page, but how do I find out within that event how far they have scrolled? In this case, the percentage particularly is what's important. I'm not particularly worried about a solution for IE6.

Do any of the major frameworks (Dojo, jQuery, Prototype, Mootools) expose this in a simple cross-browser compatible way?

Cheers,

+1  A: 

Using jQuery

$(window).scrollTop();

will get you the scroll position, you can then work out from there what the percentage is based on the window height.

There is also a standard DOM property scrollTop that you can use like document.body.scrollTop however I'm not sure how this behaves cross-browser, I would assume if there are inconsistencies then the jQuery method accounts for these.

roryf
Is window height going to return me the height of the window (e.g. 768px for a standard resolution) or the height of the content (variable, depending on the amount of text in the page).
majelbstoat
window.height will return height of viewport, document.height will return height of content. Just tested these in IE without success, but $(document).height() does work cross-browser
roryf
A: 

This should do the trick, no libraries required:

function currentScrollPercentage()
{
    return ((document.documentElement.scrollTop + document.body.scrollTop) / (document.documentElement.scrollHeight - document.documentElement.clientHeight) * 100);
}
Mark B
This is getting close, thanks. However, in Chrome at least, both document.documentElement.scrollHeight and document.documentElement.clientHeight equate to the same value (height of the content), so the denominator is always zero.
majelbstoat
Ah, yes: edited the code now, try that! There's an odd issue in WebKit browsers, see here: http://code.google.com/p/chromium/issues/detail?id=2891
Mark B
Still no worky :/ I'm pretty sure the error is on the bottom half of the division. (document.documentElement.scrollHeight - document.documentElement.clientHeight) always equals zero in Chrome. Seems like a bug?
majelbstoat
Hmm, just tested and it works fine in Chrome, IE and Firefox for me. Are you using the official latest version of Chrome?
Mark B
Chrome version 5.0.335.1 beta. The problem is definitely that the denominator evaluates to zero, because the function (exactly as copied and pasted) always returns Infinity :/
majelbstoat
Found a solution using Dojo. Definitely on the right track here though - just a question mark over what different browsers consider "clientHeight" to mean, I think.
majelbstoat
Thanks for your pointers on this. In case you're interested in what this was being used for: http://jamietalbot.com/projects/css/morning-sunset/ . It's nice sometimes to see the real world effects of potentially academic questions and answers... (But feel free to delete this comment if you think it's inappropriate self-promotion).
majelbstoat
A: 

If you're using Dojo, you can do the following:

var vp = dijit.getViewport();
return (vp.t / (document.documentElement.scrollHeight - vp.h));

Which will return a value between 0 and 1.

majelbstoat