Page scrolling is one of the least-standardized properties in JavaScript: three variations are now in use by different versions of different browsers. But with a few careful object tests, we can reliably get a consistent value.
Solution
There are three ways of getting this information. We'll use object tests on each approach, to determine the level of support available:
Example 7.8. get-scrolling-position.js (excerpt)
function getScrollingPosition()
{
var position = [0, 0];
if (typeof window.pageYOffset != 'undefined')
{
position = [
window.pageXOffset,
window.pageYOffset
];
}
else if (typeof document.documentElement.scrollTop
!= 'undefined' && document.documentElement.scrollTop > 0)
{
position = [
document.documentElement.scrollLeft,
document.documentElement.scrollTop
];
}
else if (typeof document.body.scrollTop != 'undefined')
{
position = [
document.body.scrollLeft,
document.body.scrollTop
];
}
return position;
}
The function can now be called as required. Here's a simple demonstration, using a window.onscroll
event handler, that gets the figures and writes them to the title bar:
Example 7.9. get-scrolling-position.js (excerpt)
window.onscroll = function()
{
var scrollpos = getScrollingPosition();
document.title = 'left=' + scrollpos[0] + ' top=' +
scrollpos[1];
};
The Problem with scroll
scroll
is not the most reliable of events: it may not fire at all in Konqueror or Safari 1.0, or when the user navigates with a mouse wheel in Firefox. And if it does fire, it may do so continually and rapidly (as it does in Internet Explorer), which can be slow and inefficient if the scripting you set to respond to the event is very complex.
If you have difficulties of this kind, you may find it better to use the setInterval
function instead of an onscroll event handler. setInterval
will allow you to call the function at a predictable interval, rather than in response to an event.
window.setInterval(function()
{
var scrollpos = getScrollingPosition();
document.title = 'left=' + scrollpos[0] + ' top=' +
scrollpos[1];
}, 250);
Discussion
The only real complication here is that IE 5 actually does recognize the documentElement.scrollTop
property, but its value is always zero, so we have to check the value as well as looking for the existence of the property.
Otherwise, it doesn't really matter to us which browser is using which property; all that matters is that our script gets through one of the compatibility tests and returns a useful value. However, the properties used by each browser are shown here for reference:
-
window.pageYOffset
is used by Firefox and other Mozilla browsers, Safari, Konqueror, and Opera.
-
document.documentElement.scrollTop
is used by IE 6 in standards-compliant mode.
-
document.body.scrollTop
is used by IE 5, and IE 6 in "Quirks" mode.
This list doesn't tell the complete story, but it's intended primarily to describe the ordering of the tests. More recent Mozilla browsers (such as Firefox) also support documentElement.scrollTop
and body.scrollTop
, by the same rendering mode rules as IE 6. Safari and Konqueror support body.scrollTop
in either mode. Opera supports all three properties in any mode!
But none of this is important for you to know -- browser vendors add these multiple properties to allow for scripts that are unaware of one property or another, not to provide arbitrary choices for the sake of it. From our perspective, the important point is to settle on a set of compatibility tests that ensures our script will work as widely as possible.
Get more information
Tags: scroll position, javascript, javascript code, mouse position, browsers, page scrolling, scrolltop