Mouse position - coords

Nate Reed nate at SEASTAR.JPL.NASA.GOV
Thu Aug 25 17:45:11 EDT 2005


If I understand correctly, this is a two-part question, how to get the 
cursor coordinates and how to get the position of the image.

Getting the position of an element on the page is kind of tricky.

See: 
http://www.quirksmode.org/js/findpos.html

offsetLeft and offsetTop will give you the position relative to the
element's parent container.  This is implemented differently in different
browsers.  So, you have to find the position of the parent container and
add that to the offsets.  The above link explains and provides some 
functions to find position.

For the mouse coordinates I use:
 if (e.pageX) {
		  newx = e.pageX;
        	  newy = e.pageY;
		}
		else {
		  newx = e.clientX + document.body.scrollLeft
		  newy = e.clientY + document.body.scrollTop		
		}
Nate

 On Thu, 25 Aug 2005, Jirka Cajthaml 
wrote:

> Hi,
> 
> I use mapserver on my web page. The map is generated as image by CGI 
> mapserver
> and I want to show coordinates on mouse over this map. So I need pixel 
> coordinates
> of image (it is part of a page - table). Do you know how to solve this 
> in Javacript?
> 
> I solved this for IE, but I dont know how to solve this for Mozilla...
> 
> my script:
> 
> function MYS() {
> if (navigator.appName == 'Netscape') {
>           document.captureEvents(Event.MOUSEMOVE);
>       document.onmousemove = NNMouseMove(e);   
> }
> if (document.all) {
>           MSMouseMove();
>         }
> }
> 
> function NNMouseMove(e) {
>             document.mapserv.X.value = e.pageX;
>             document.mapserv.Y.value = e.pageY;
> }
> 
> function MSMouseMove() {
>             document.mapserv.X.value = event.offsetX;
>             document.mapserv.Y.value = event.offsetY;
> }
> 
> and in HTML there is
> ..
> <input type="image" name="img" src="[img]" width="600" height="420" 
> border="0" onmousemove=MYS();>
> ...
> 
> 
> It works for IE - it shows pixel coords of image, but in Mozilla it 
> shows coords of page (I dont know how
> can I get coords of upperleft corner of image realtive to page...)
> 
> Thanks for any reaction
> 
> Jiri Cajthaml
> 

-- 
Nate Reed
Physical Oceanography DAAC
Jet Propulsion Lab / Raytheon ITSS
(626)744-5528  nate at seastar.jpl.nasa.gov



More information about the mapserver-users mailing list