473,657 Members | 2,479 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How Can I Find Mouse Position Inside of a Div Tag?

I have a DIV element. How can I find mouse position (top and left)
inside of this DIV?

<div onMouseMove="ge tPositions();"
style="width:20 0px;height:100p x"></div>

function getPositions(ev ){
??????????????? ?
}

Jan 5 '07 #1
4 26483
vu******@gmail. com wrote:
I have a DIV element. How can I find mouse position (top and left)
inside of this DIV?

<div onMouseMove="ge tPositions();"
style="width:20 0px;height:100p x"></div>

function getPositions(ev ){
??????????????? ?
}
function getPositions(ev ) {
if (ev == null) { ev = window.event }
_mouseX = ev.clientX;
_mouseY = ev.clientY;
}

--
http://www.hunlock.com -- Musings in Javascript, CSS.
$FA
Jan 5 '07 #2

vu******@gmail. com wrote:
I have a DIV element. How can I find mouse position (top and left)
inside of this DIV?
This is difficult. The event object has clientX and clientY properties
which describe where the event occurred relative to the browser window
in pixels. If you then add the amount by which the page has been
scrolled then you know where the event occurred relative to the web
page. Now if you can determine the position of the div relative to the
web page then you will can subtract the position of the div from the
position of the event and they you have your answer. Determining the
position of the mouse relative to the page is relatively easy.
Determining the position of the div relative to the page may be very
difficult in general but easy in certain cases.

Peter

Jan 6 '07 #3
Peter Michaux wrote:
vu******@gmail. com wrote:
>I have a DIV element. How can I find mouse position (top and left)
inside of this DIV?

This is difficult. The event object has clientX and clientY properties
which describe where the event occurred relative to the browser window
in pixels. If you then add the amount by which the page has been
scrolled then you know where the event occurred relative to the web
page. Now if you can determine the position of the div relative to the
web page then you will can subtract the position of the div from the
position of the event and they you have your answer. Determining the
position of the mouse relative to the page is relatively easy.
Determining the position of the div relative to the page may be very
difficult in general but easy in certain cases.

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

Quirksmode has a function which returns the top and left position of the
target element, that can be subtracted from the current mouse position
to obtain relative positions. It's a useful clip, I think the
javascript faqs have something similar too.

--
http://www.hunlock.com -- Musings in Javascript, CSS.
$FA
Jan 6 '07 #4
pcx99 wrote:
>
http://www.quirksmode.org/js/findpos.html

Quirksmode has a function which returns the top and left position of the
target element, that can be subtracted from the current mouse position
to obtain relative positions. It's a useful clip, I think the
javascript faqs have something similar too.
The quirksmode script will only work for very limited set of simple
pages where there are not scrolled elements and other various trickier
situations. The Yahoo! UI, ajaxtoolbox, and Flanagan's book have
better but not generally applicable position reporting functions which
is a difficult problem in general.

Peter

Jan 6 '07 #5

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

Similar topics

2
16464
by: madmike | last post by:
Hi, I have an iframe with some javascipt that gets the mouse x and y position like this: //inside the iframe's onmousemove call back funciton xMousePos = window.event.clientX + document.body.scrollLeft; yMousePos = window.event.clientY + document.body.scrollTop; This gets the x and y position inside the iframe. But what I want is
13
3139
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
16
1953
by: Niels Jensen | last post by:
I have been developing a little project which draw's a hexgrid on a panel within a form similar to this - it:s used as a client from a e-mail based strategy game: ____ ____ / \ / \ / (0,0) \____/ (2,0) \____/ \ / \ / \ \____/(1,1) \____/ (3,1) \_ / \ / \ / (0,1) \____/ (2,1) \____/
2
4999
by: Sam | last post by:
Hi, I can't figure out how to detect when my mouse cursor leaves a panel control. It should not trigger the event (or do anything) when the mouse leave the panel but still is over a control that is contained by the panel. I've done this : Protected Overrides Sub OnMouseLeave(ByVal e As System.EventArgs) If Cursor.Position.X > Me.Location.X + Me.Width Or Cursor.Position.Y > Me.Location.Y + Me.Height _
2
19584
by: quickcur | last post by:
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
3
5176
by: wanwan | last post by:
I made a game with a window form that needs to record the mouse position to an array at 100 samples per second, so I use the mouse move event to do the job. The problem is the mouse move event is raised by my software at max 30 times per second. I have determined I should use a timer event on every 10 msec to raise the mouse event, but then I need the same mouse event that is sent to the mouse move function in the mouse move event how...
4
2893
by: =?Utf-8?B?Unlhbg==?= | last post by:
I have a winform containing a scrollable panel and a groupbox inside the panel. There is a button inside the groupbox. When that button is clicked; how do I capture and display the X and Y coordinates in a messagebox?
0
8421
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
8844
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8518
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,...
1
6177
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5643
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();...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2743
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1971
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1734
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.