Thursday, September 07, 2006

Getting the Scrolling position using Javascript


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

8 Comments:

At 3:21 AM, Blogger Jay said...

Excellent post. The best post I found about scrolling so far.

Thanks for the answer

 
At 2:53 AM, Blogger Bhupesh Mohan said...

Thanks my Friend,,,, I am really glad to see you in this,,,, your scrolling idea gave me a wonderful solutions to my task......

Once again Thank you very much...

 
At 3:39 AM, Blogger amatoryazilim said...

Thanks my friend.

"document.body.scrollTop" is wrong.

and I saw yours code than try

"document.documentElement.scrollTop"
its right.

Have a nice day thanks again.

 
At 2:38 AM, Blogger himalayan_wanderer said...

Millions of thanks my friend :) your post has solved my scroll problems forever... Keep up the good work :)

 
At 6:45 AM, Blogger Unknown said...

HI,

Great Post. I am currently working on iPad enhancement and i need a div to remain at the bottom of the window while the content can scroll down with fingers. The code i am using is:
$('div#myDock').css({ 'position': 'absolute', 'top': $(window).height() - $('#myDock').height() + document.documentElement.scrollTop + 'px' })

and it is not working for iPad. Any help will be highly appreciated. Release is near :(

 
At 6:47 AM, Blogger Unknown said...

HI,

Great Post. I am currently working on iPad enhancement and i need a div to remain at the bottom of the window while the content can scroll down with fingers. The code i am using is:
$('div#myDock').css({ 'position': 'absolute', 'top': $(window).height() - $('#myDock').height() + document.documentElement.scrollTop + 'px' })

and it is not working for iPad. Any help will be highly appreciated. Release is near :(

 
At 4:16 AM, Blogger Unknown said...

simply awesome saved a lot of time :-D
thanks a lot :-)

 
At 4:16 AM, Blogger Unknown said...

simply awesome saved a lot of time :-D
thanks a lot :-)

 

Post a Comment

<< Home