NN 6, IE 5
You want to read the x,y coordinates of a click (or other) event with respect to the coordinate plane of the entire page or just the element being clicked.
This recipe presents solutions for two situations because each has
its own idiosyncrasies when trying to merge event coordinates with
page coordinates typically used for positioning elements. The same
scenario is assumed: a user clicks somewhere on the page to point to
a location where a positioned element is to be placed. Imagine the
user clicking on a map to position an arrow graphic. Differences
accrue as to whether the positioning is relative to the page or to
the rectangle occupied by a positioned element. Use one of two
functions described in the Discussion,
, to obtain coordinates that coincide
with the event's coordinates. Both functions return
an object with
properties whose values represent position coordinates.
The basis for this example's user interface is one
of two versions of the
function, which relies on the
function of the DHTML API (Recipe
13.3). When the user clicks anywhere within the scope of the event
binding with the Shift key down, the top-left corner of a positioned
element is brought to the click spot.
The first case we'll cover obtains coordinates relative to the space occupied by the entire page, so you can position the top-left corner ...