473,386 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

mouse click location

Hi, I have html like this:

<div id="myCanvas" style="border:10px,
black;position:relative;height:250px;width:100%;">
<img id="p" src="p.jpg">
</div>

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?

Thanks,

qq

Jun 29 '06 #1
2 19563
Jim
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
----------------------------------
<html>
<head>
<title>mouseTracker and event trigger based on mouse
coordinates</title>
<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 ;
}
</script>
</head>
<body bgcolor="black" color="white" onMousemove="mCo(event)">
<div style=" background-color:teal; color:white; border:3px gray ridge;
width:300px;
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>
<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;
</div>
</body>
</html>
----------------------------------

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:
<URL:http://www.quirksmode.org/js/events_properties.html>

'mouse' events, including mouse move, over elements, etc.
<URL:http://www.quirksmode.org/js/events_mouse.html>
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
}

[...]

--
Rob

Jun 30 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: John Champaign | last post by:
Hi all, I'm working on an educational applet for a child with special needs. He's got a bit of a trick to make my life more difficult... To interact with the applet he needs to click on...
9
by: punkin | last post by:
I am trying to catch mouse position on the entire screen by dynamically generating mouse click event at every 100 ms. My code only works for IEs but not any Netscape or Gecko-based browsers. The...
5
by: Charles Law | last post by:
Sorry for reposting this question, but I did not get a single answer last time, and I'm sure you guys must have some thoughts on the matter. I have a user control which can be dragged and dropped...
5
by: Sameh Ahmed | last post by:
Hello there how do I send a mouse click to a specific set of coordinates in the desktop? what do I need to search for? any info is greatly appreciated. Regards Sameh
6
by: jcrouse | last post by:
I have the following mouse events assigned to a label control. Is the a way I can tell which mouse button the users has clicked with? Private Sub lblP1JoyUp_Click(ByVal sender As System.Object,...
3
by: jcrouse | last post by:
I have created a form designer type application (with a lot of you peoples helpJ). It has label controls that are draggable at runtime. The user is also allowed to change some properties such as...
13
by: Lars Netzel | last post by:
Hi! I have a round area which I want to be able to move the mouse over and fire off events... how do I do that? I have drawn a FillPie Graphics and I feel that there has to be a way of...
5
by: moonie | last post by:
I have an msn style c# windows application with a form and panel on it. A news list is drawn from the database and dynamically added via labels, link lables during form loading. In this...
3
by: =?Utf-8?B?SmVycnk=?= | last post by:
I have the following code and it returns the x and y coordinates in the text box, but when I click the mouse on say 362,61 does not bring up form2.Any suggestions? Thanks, Jerry Private Sub...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.