Let's say I have a page with some kind of image on it. Suppose we want to create a control which performs zoom on the image according to a user defined borders (User drags the mouse from one point on the image to another and creates a box). In order to do this we need to use events and scripts so we could know what area in the image the user selected.
When adding event handlers to client events in a Html or ASP.Net page you can use an object called
event. Among it's properties there are a couple which refers to the position of the mouse when the event happens.
When I tried to debug such control I was very confused with these properties,so I read MSDN, which doesn't explains it very well and I decided to try and explain myself.
screenX & screenY
Returns the position of the mouse relative to the screen of the client. In computer screen the top-left corner is considered to be (0,0) and the bottom-right corner of the screen is (maxResolutionX,maxResolutionY),
for example (1280,1024).
So if the mouse moves down and right these values will increase according to these axis.
clientX & clientY
Returns the position of the mouse relative to the client area of the browser window. What is the client area? This is the area in which the content of the web site is displayed (see the picture below).
offsetX & offsetY
Returns the position of the mouse relative to the position of the object which triggered the event. That means (offsetX,offsetY) = (0,0) is in the top-left position of the object on which the event handler was defined.
In the picture below there is a mousemove event on the green div so (0,0) will be at the top-left corner of this div.
x & y
These are the most complicated ones. If the object which triggered the event is relatively positioned (has
position:relative in it's style) then (0,0) will be at the top-left corner of the object (Like the purple div in the image below which is defined as positioned relative).
If the object is not relatively position then (0,0) will be in the top-left corner of the closest parent of the object which is relatively positioned (that means that is the parent of the object is relatively positioned then (0,0) will be at the parent's top-left corner).
If none of the parents of the object throw out the hierarchy is relatively positioned then (0,0) will be the same as ClientX/Y.
Like they say - A picture worth a thousand words:
In the image you can see all the (0,0) points of each position property.
The image is taken from an example page I've written, in which I've defined an
onmouemove event on two divs. The difference between them is that thepurple div is relatively positioned:
Another thing you need to notice: Only the offset properties gives the real position when there is a scroll.
For example: If you move the scroll in the example by 10 pixels then offset position will move by 10 pixels (in oppose to the other properties which will only give you the absolute position in the page). This has an exception: If the object is relatively positioned then the (x,y) properties will also given you the real position.