[thelist] Scroll Position (Javascript)

Peter-Paul Koch gassinaumasis at hotmail.com
Thu Nov 20 07:02:25 CST 2003



>I think this should be easy but I'm stuck on it today.
>
>I capture a certain click event and I can figure out the coordinates of 
>that
>click. I want now to determine how much VISIBLE space there is in the
>browser between that click and the bottom of the VISIBLE area of the
>browser.
>
>Does this make sense? In other words, I want to know how many pixels below
>the click event the user is able to see in his browser when the click event
>happens.

Take the total height of the page (see 
http://www.quirksmode.org/viewport/compatibility.html)

var y;
var test1 = document.body.scrollHeight;
var test2 = document.body.offsetHeight
if (test1 > test2) // all but Explorer Mac
{
	y = document.body.scrollHeight;
}
else // Explorer Mac;
     //would also work in Explorer 6 Strict, Mozilla and Safari
{
	y = document.body.offsetHeight;
}

Then take the y-coordinate of the click event (see 
http://www.quirksmode.org/js/events_compinfo.html#prop)

	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY)
	{
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY)
	{
		posy = e.clientY + document.body.scrollTop;
	}

Subtract

var pixelsBelowClick = y - posY;

You've got it.

-------------------------------------------------------------------
ppk, freelance web developer
Interaction, copywriting, JavaScript, integration
http://www.quirksmode.org/
Column "Keep it Simple": http://www.digital-web.com/columns/keepitsimple/
------------------------------------------------------------------

_________________________________________________________________
Play online games with your friends with MSN Messenger 
http://messenger.msn.nl/



More information about the thelist mailing list