Forum Moderators: open

Message Too Old, No Replies

Retrieving viewport scroll position?

         

antonyw

8:10 am on Dec 17, 2007 (gmt 0)

10+ Year Member



Hello!

I would like to popup an alert box which is a div, rather than a new window. My only problem is with pages that are deep enough to cause the browser to create a vertical scroll bar because in order to get my div on the visible viewport I need to know how far the page is currently scrolled - eg the y coord of the viewport.

One sees behaviour out there that suggests this is quite possible, but it seems like difficult info to get hold of.

Any ideas?

tia

tony

Dabrowski

1:57 pm on Dec 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Actually it's very easy. There's a property called 'scrollTop' which you can access.

Use it like this:

var myDiv = document.getElementById( "scrolling_div"); 
alert( myDiv.scrollTop);

In your case I imagine you can do:

alert( document.body.scrollTop);

Not sure on the browser compatibility on this though, I know it works in IE, pretty sure it works in FF, not sure about anything else though.

antonyw

9:18 pm on Dec 18, 2007 (gmt 0)

10+ Year Member



Brilliant! Many thanks :)

tony