469,270 Members | 1,753 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,270 developers. It's quick & easy.

mouse click location

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
2 19287
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 yahoo.com; 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.target) ? evt.target : ((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 = "http://www.yahoo.com";

// 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
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.

Similar topics

5 posts views Thread by John Champaign | last post: by
9 posts views Thread by punkin | last post: by
13 posts views Thread by Lars Netzel | last post: by
5 posts views Thread by moonie | last post: by
3 posts views Thread by =?Utf-8?B?SmVycnk=?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.