473,795 Members | 2,865 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

mouse click location

Hi, I have html like this:

<div id="myCanvas" style="border:1 0px,
black;position: relative;height :250px;width:10 0%;">
<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 19593
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>mouseTra cker and event trigger based on mouse
coordinates</title>
<script language="javas cript">
function mCo(evt ){
// create cross-browser event detector:
var node = (evt.target) ? evt.target : ((evt.srcElemen t)
?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="mC o(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:transpare nt; 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_properti es.html>

'mouse' events, including mouse move, over elements, etc.
<URL:http://www.quirksmode. org/js/events_mouse.ht ml>
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.s crollLeft;
posy = e.clientY + document.body.s crollTop;
}
// 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
6230
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 buttons, which is fine most of the time (he comes from a Mac environment, so I accept mouse clicks from the right or left button when he's working on the PC). But every once in a while, he'll press and hold the right mouse button, move onto a JButton,...
9
22755
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 following are the problems and I hope that there is someone who can enlighten me or give me some pointers. Also, my testing code is attached at the end. And please don't ask me why I am doing this - it is one of functional requirements by all...
5
3723
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 onto a form in my application when it is running. I allow it to be clicked and dragged to a new location on the form. However, the user control has a check box on it, and if the user clicks over the checkbox to drag the user control, the check...
5
2834
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
6998
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, ByVal e As System.EventArgs) Handles lblP1JoyUp.Click If bMouseMove = False Then If bCtrlKey = True Then
3
2076
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 forecolor, backcolor and font. The labels also are rotatable and the text can also be flipped 180 degrees (the flipped text part is still being worked on). I have one context menu for all 30 labels that allows for the property changes to the labels....
13
3156
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 getting to know if the mouse is over that area since I have the coordinates to paint the Pie but I don't know where to start or what to look for really. Best Regard
5
2395
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 application at run time this list should be generated again as the filtering criteria changes. So I clear the controls(labels) on the panel, get the list from the database and generate the list again. The list is created and it works fine. But sometimes...
3
1817
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 PictureBox1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseClick Dim mouseX As Integer = e.X Dim mouseY As Integer = e.Y
0
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9519
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10213
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10163
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9040
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6780
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3722
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.