By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,729 Members | 1,398 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,729 IT Pros & Developers. It's quick & easy.

mouse click location

P: n/a
Hi, I have html like this:

<div id="myCanvas" style="border:10px,
<img id="p" src="p.jpg">

When user click the mosue, I would like

1. Test if the mouse location is on top of the image
2. Find the relative mouse location respect to the top-left corner of
the image.
3. If all yes, do something.

I am using event.x and event.y but I do not know the x and y is
relative location or what?



Jun 29 '06 #1
Share this Question
Share on Google+
2 Replies

P: n/a
Quick Cur,
Here's a MSIE and FireFox compatible version of the msie-only script i
sent you privately:
As you'll see, the mouse coordinates are posted in the status bar; when
they broach the inside of the red-dashed div box, the script will
trigger and re-locate the page to; for your needs, the
coordinates can equal the top left corner of your image, if it is
absolutely positioned on the page. (ie: you may have to detect the
image position if it is dynamically placed or relative to other
components). HTH - Jim
<title>mouseTracker and event trigger based on mouse
<script language="javascript">
function mCo(evt ){
// create cross-browser event detector:
var node = ( ? : ((evt.srcElement)
?evt.srcElement : null );
evt = (evt) ? evt : ((event) ? event : null);

// create mouse coordinates objects:
xpo = evt.clientX;
ypo = evt.clientY;

// Set left-right, top-bottom params within 'if' stmt to equal
// the bounded box (or image) you want to detect:
if( (xpo >= 100) && (xpo <= 200 ) && (ypo >= 100) && (ypo <= 200) ){
window.location = "";

// post all coordinates to the status bar while the mouse is moving:
window.status= "X-coordinate is: " + xpo + " Y-coordinate is: " +
ypo ;
<body bgcolor="black" color="white" onMousemove="mCo(event)">
<div style=" background-color:teal; color:white; border:3px gray ridge;
height:50px; z-index:1" id='the_box'>
Below is a red target area. Your mouse position will be tracked (see
the mouse coordinates in the status bar area) so that if your mouse
moves into the 'target' area, the window.location will change.
<div style="position:absolute; left:100; top:100;
background-color:transparent; border:1px red dashed; width:100px;
height:100px; z-index:2" id='targetarea'>&nbsp;

Jun 30 '06 #2

P: n/a
Jim wrote:
Quick Cur,
Here's a MSIE and FireFox compatible version of the msie-only script i
sent you privately:

Please don't top post, it destroys the flow of the thread. Your script
and explanations are very ordinary, the OP will get a much better idea
of events and their properties by reading quirksmode:

Event properties, including location:

'mouse' events, including mouse move, over elements, etc.
For the record, quirksmode suggests getting cursor coordinates using:

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



Jun 30 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.