Page 1 of 1

XY-Position of the mousepointer

Posted: Wed Dec 11, 2002 4:05 pm
by Heavy
I have seen some page where an image follows the xy-position of mouse pointer in a frame.
What javascript properties are those that make that possible?
I know it works with IE, but I don't know how.
Is there a way to do it in Netscape 7?

In this case I am specifically interrested in the property that returns the x and y values. I already know how to do image positioning.

For example:

Code: Select all

var x=window.pointer.x
var y=window.pointer.y
...or something

Posted: Thu Dec 12, 2002 4:41 am
by Heavy
Ok sorry... I found it myself.

In return I tell the world how it can be done:

Code: Select all

<?
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

$BrowserIsIE = (strpos($_SERVER['HTTP_USER_AGENT'],"MSIE")?1:0);
?>
<html>
	<head>
		<script>
			var NowX = 0;
			var NowY = 0;

<? if ($BrowserIsIE){
?>
// Captures mouse position for MS-IE:
			function GetMouseCoords()
			{
				NowX = window.event.clientX
				NowY = window.event.clientY
			}
<?
}else{
?>
// Captures mouse position for standards compatible browsers:
			function GetMouseCoords(evnt) {
				NowX = evnt.layerX
				NowY = evnt.layerY
				return true;
			}

			document.onmousemove = GetMouseCoords;
<?
}
?>
		</script>
	</head>
	<body <?=($BrowserIsIE?"onmousemove="GetMouseCoords()"":"")?>>
	<div onclick="alert(NowX + ':' + NowY)" style="cursor:pointer;cursor:hand">Click Me in different coordinates</div>
	</body>
</html>
It has been tested and works for Netscape7 and MS IE 6.0.2800.1106