majelbstoat majelbstoat - 12 days ago 4x
Javascript Question

Cross-Browser Method to Determine Vertical Scroll Percentage in Javascript

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?



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.