[Javascript] True element coordinates on mouseevent.

Peter-Paul Koch gassinaumasis at hotmail.com
Fri Nov 7 05:10:33 CST 2003



> > Is detecting the true element mouse coordinates (the exact coordinates
> > of the mouse pointer on the element it's currently hovering,
> > [event.]layerX/layerY (only absolutely position/Mozilla) or
> > [event.]offsetX/offsetY(IE/Safari only)) still impossible?

Nope.

function doSomething(e)
{
	var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY)
	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY)
	{
		posx = e.clientX + document.body.scrollLeft;
		posy = e.clientY + document.body.scrollTop;
	}
	// posx and posy contain the mouse position relative to the document
	// Do something with this information
}

I remember
> > trying to do this a few years back, when I found out that it only worked
> > for elements with position: absolute, and that makes the function
> > completely useless and very stupid indeed.

That was in Netscape 4.

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

_________________________________________________________________
The new MSN 8: advanced junk mail protection and 2 months FREE* 
http://join.msn.com/?page=features/junkmail




More information about the Javascript mailing list